操作场景
通过按钮创建数据库表、删除数据库表、向表中插入数据、从数据表查询数据
编写步骤
step-1:新建android项目:SQ01
step-2:编写类Ac01
1 | package com.bitnanke; |
2 | |
3 | import android.app.Activity; |
4 | import android.content.Context; |
5 | import android.database.Cursor; |
6 | import android.database.SQLException; |
7 | import android.database.sqlite.SQLiteDatabase; |
8 | import android.database.sqlite.SQLiteOpenHelper; |
9 | import android.os.Bundle; |
10 | import android.util.Log; |
11 | import android.view.View; |
12 | import android.view.View.OnClickListener; |
13 | import android.widget.Button; |
14 | import android.widget.LinearLayout; |
15 | |
16 | public class Ac01 extends Activity implements OnClickListener{ |
17 | private final int WC=LinearLayout.LayoutParams.WRAP_CONTENT; |
18 | private static final String DB_NAME="StudDB.db"; |
19 | private static final int DB_VERSION=2; |
20 | private Cursor cur; |
21 | private Button btn,btn2,btn3,btn4,btn5; |
22 | private static class DatabaseHelper extends SQLiteOpenHelper{ |
23 | |
24 | /** |
25 | * Description: |
26 | * |
27 | * @param: |
28 | * @param context |
29 | * @param name |
30 | * @param factory |
31 | * @param version |
32 | **/ |
33 | public DatabaseHelper(Context context) { |
34 | super(context, DB_NAME, null, DB_VERSION); |
35 | // TODO Auto-generated constructor stub |
36 | } |
37 | |
38 | /**Description: |
39 | * |
40 | * @param db |
41 | * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase) |
42 | */ |
43 | @Override |
44 | public void onCreate(SQLiteDatabase db) { |
45 | // TODO Auto-generated method stub |
46 | |
47 | } |
48 | |
49 | /**Description: |
50 | * |
51 | * @param db |
52 | * @param oldVersion |
53 | * @param newVersion |
54 | * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int) |
55 | */ |
56 | @Override |
57 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
58 | // TODO Auto-generated method stub |
59 | |
60 | } |
61 | |
62 | } |
63 | /** Called when the activity is first created. */ |
64 | @Override |
65 | public void onCreate(Bundle savedInstanceState) { |
66 | super.onCreate(savedInstanceState); |
67 | LinearLayout layout=new LinearLayout(this); |
68 | layout.setOrientation(LinearLayout.VERTICAL); |
69 | |
70 | btn=new Button(this); |
71 | btn.setText("Create student table"); |
72 | btn.setOnClickListener(this); |
73 | layout.addView(btn, new LinearLayout.LayoutParams(WC,WC)); |
74 | |
75 | btn2=new Button(this); |
76 | btn2.setText("Drop student table"); |
77 | btn2.setOnClickListener(this); |
78 | layout.addView(btn2, new LinearLayout.LayoutParams(WC,WC)); |
79 | |
80 | btn3=new Button(this); |
81 | btn3.setText("Insert a record"); |
82 | btn3.setOnClickListener(this); |
83 | layout.addView(btn3, new LinearLayout.LayoutParams(WC,WC)); |
84 | |
85 | btn4=new Button(this); |
86 | btn4.setText("Select all"); |
87 | btn4.setOnClickListener(this); |
88 | layout.addView(btn4, new LinearLayout.LayoutParams(WC,WC)); |
89 | |
90 | btn5=new Button(this); |
91 | btn5.setText("Exit"); |
92 | btn5.setOnClickListener(this); |
93 | layout.addView(btn5, new LinearLayout.LayoutParams(WC,WC)); |
94 | |
95 | setContentView(layout); |
96 | } |
97 | |
98 | private DatabaseHelper mOpenHelper; |
99 | |
100 | /**Description: |
101 | * |
102 | * @param v |
103 | * @see android.view.View.OnClickListener#onClick(android.view.View) |
104 | */ |
105 | public void onClick(View v) { |
106 | // TODO Auto-generated method stub |
107 | mOpenHelper=new DatabaseHelper(v.getContext()); |
108 | if(v==btn){ |
109 | SQLiteDatabase db=mOpenHelper.getWritableDatabase(); |
110 | String sql="create table student(stud_no text not null,stud_name text);"; |
111 | try { |
112 | db.execSQL(sql); |
113 | setTitle("Create table ok"); |
114 | } catch (SQLException e) { |
115 | // TODO: handle exception |
116 | Log.e("ERROR", e.toString()); |
117 | setTitle("Create table error"); |
118 | } |
119 | } |
120 | if(v==btn2){ |
121 | SQLiteDatabase db=mOpenHelper.getWritableDatabase(); |
122 | String sql="drop table student"; |
123 | try { |
124 | db.execSQL(sql); |
125 | setTitle("Drop table ok"); |
126 | } catch (SQLException e) { |
127 | // TODO: handle exception |
128 | Log.e("ERROR", e.toString()); |
129 | setTitle("Drop table error"); |
130 | } |
131 | } |
132 | if(v==btn3){ |
133 | SQLiteDatabase db=mOpenHelper.getWritableDatabase(); |
134 | String sql_1="insert into student values('S108','Lily Chen');"; |
135 | String sql_2="insert into student values('S201','Tom Kao');"; |
136 | String sql_3="insert into student values('S333','Peter Rabbit');"; |
137 | try { |
138 | db.execSQL(sql_1); |
139 | db.execSQL(sql_2); |
140 | db.execSQL(sql_3); |
141 | setTitle("insert records ok"); |
142 | } catch (SQLException e) { |
143 | // TODO: handle exception |
144 | Log.e("ERROR", e.toString()); |
145 | setTitle("Drop table error"); |
146 | } |
147 | } |
148 | if(v==btn4){ |
149 | SQLiteDatabase db=mOpenHelper.getReadableDatabase(); |
150 | String col[]={"stud_no","stud_name"}; |
151 | cur=db.query("student", col, null, null, null, null, null); |
152 | Integer n=cur.getCount(); |
153 | String ss=Integer.toString(n); |
154 | setTitle(ss+" records"); |
155 | cur.moveToFirst(); |
156 | } |
157 | if(v==btn5){ |
158 | finish(); |
159 | } |
160 | } |
161 | } |
总结
1、实现抽象类SQLiteOpenHelper
需提供数据库名以及版本号,在调用构造器函数时会使用到这两个参数
a、构造器:SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
该构造器创建一个帮助类对象用于管理数据库
b、调用实现类的getReadableDatabase()或者getWritableDatabase()获得数据库对象(SQLiteDateBase)
注意:在调用这两个函数时,数据库才真正的创建或被打开
c、public abstract void onCreate (SQLiteDatabase db)
当数据库第一次创建时调用该方法,可以包含创建表、初始化表此类操作
3、使用数据库对象创建表、删除表、执行SQL语句
4、数据库查询返回游标对象(Cursor),它提供对结果的随机读写访问
另:
1、向数据库插入数据,可以使用ContentValues对象
ContentValues cv=new ContentValues;
cv.put("stud_no","S108");
cv.put("stud_name","Lily Chen");
db.insert("student",null,cv);
2、在文件AndroidManifest.xml中可以存在多个Activity被定义为启动类,但在Android平台上会根据启动类的多少生成多个同名应用
例如:本例工程SQ01,如果编写两个Activity并且都配置为启动类,那么在Android上会生成两个名为SQ01的APP