Android SQLite数据库操作
布局文件
main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/createDatabase"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="createDatabase"/>
12 <Button
13 android:id="@+id/updateDatabase"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:text="updateDatabase"/>
17 <Button
18 android:id="@+id/insert"
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:text="insert"/>
22 <Button
23 android:id="@+id/update"
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"
26 android:text="update"/>
27 <Button
28 android:id="@+id/query"
29 android:layout_width="fill_parent"
30 android:layout_height="wrap_content"
31 android:text="query"/>
32 <Button
33 android:id="@+id/delete"
34 android:layout_width="fill_parent"
35 android:layout_height="wrap_content"
36 android:text="delete"/>
37 <Button
38 android:id="@+id/droptable"
39 android:layout_width="fill_parent"
40 android:layout_height="wrap_content"
41 android:text="drop table"/>
42 </LinearLayout>
DatabaseHelper.java
DatabaseHelper.java
1 package mars.sqlite.db;
2
3 import android.content.Context;
4 import android.database.sqlite.SQLiteDatabase;
5 import android.database.sqlite.SQLiteDatabase.CursorFactory;
6 import android.database.sqlite.SQLiteOpenHelper;
7
8 public class DatabaseHelper extends SQLiteOpenHelper {
9
10 private static final int VERISON=1;
11
12 public DatabaseHelper(Context context, String name, CursorFactory factory,
13 int version) {
14 super(context, name, factory, version);
15 // TODO Auto-generated constructor stub
16 }
17
18 public DatabaseHelper(Context context,String name){
19 this(context,name,DatabaseHelper.VERISON);
20 }
21
22 public DatabaseHelper(Context context,String name,int verison){
23 this(context,name,null,verison);
24 }
25
26 @Override
27 public void onCreate(SQLiteDatabase db) {
28 // TODO Auto-generated method stub
29 System.out.println("create a database");
30 db.execSQL("create table users(id int,name varchar(20))");
31 }
32
33 @Override
34 public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
35 // TODO Auto-generated method stub
36 System.out.println("update a database");
37 }
38
39 }
SQLiteOperation.java
SQLiteOperation
1 package mars.activity;
2
3 import mars.sqlite.db.DatabaseHelper;
4 import android.app.Activity;
5 import android.content.ContentValues;
6 import android.database.Cursor;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12
13 public class SQLiteOperationActivity extends Activity {
14 /** Called when the activity is first created. */
15
16 private Button createDatabase = null;
17 private Button updateDatabase = null;
18 private Button insert = null;
19 private Button update = null;
20 private Button query = null;
21 private Button delete = null;
22 private Button dropTable=null;
23
24 @Override
25 public void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.main);
28 // 创建数据库
29 createDatabase = (Button) this.findViewById(R.id.createDatabase);
30 //设置监听器
31 createDatabase.setOnClickListener(new CreateDatabaseListener());
32 // 更新数据库
33 updateDatabase = (Button) this.findViewById(R.id.updateDatabase);
34 updateDatabase.setOnClickListener(new UpdateDatabaseListener());
35 // 添加数据
36 insert = (Button) this.findViewById(R.id.insert);
37 insert.setOnClickListener(new InsertListener());
38 // 更新数据
39 update = (Button) this.findViewById(R.id.update);
40 update.setOnClickListener(new UpdateListener());
41 // 查询数据
42 query = (Button) this.findViewById(R.id.query);
43 query.setOnClickListener(new QueryListener());
44 // 删除数据
45 delete = (Button) this.findViewById(R.id.delete);
46 delete.setOnClickListener(new DeleteListener());
47 //删除表单
48 dropTable=(Button) this.findViewById(R.id.droptable);
49 dropTable.setOnClickListener(new DropTableListener());
50 }
51
52 // 创建数据库单击事件
53 class CreateDatabaseListener implements OnClickListener {
54 @Override
55 public void onClick(View v) {
56 DatabaseHelper dbHelper = new DatabaseHelper(
57 SQLiteOperationActivity.this, "sqlite_test_db");
58 @SuppressWarnings("unused")
59 SQLiteDatabase sqlDb = dbHelper.getWritableDatabase();
60 }
61 }
62
63 // 更新数据库单击事件
64 class UpdateDatabaseListener implements OnClickListener {
65 @Override
66 public void onClick(View v) {
67 System.out.println("update database verison");
68 DatabaseHelper dbHelper = new DatabaseHelper(
69 SQLiteOperationActivity.this, "sqlite_test_db",2);
70 @SuppressWarnings("unused")
71 SQLiteDatabase sqlDb = dbHelper.getWritableDatabase();
72
73 }
74 }
75
76 // 添加数据单击事件
77 class InsertListener implements OnClickListener {
78 @Override
79 public void onClick(View v) {
80 System.out.println("insert");
81 DatabaseHelper dbHelper = new DatabaseHelper(
82 SQLiteOperationActivity.this, "sqlite_test_db");
83 SQLiteDatabase sqlDb = dbHelper.getWritableDatabase();
84 ContentValues values=new ContentValues();
85 values.put("id", 1);
86 values.put("name", "zhangsan");
87 sqlDb.insert("users", null, values);
88 }
89 }
90
91 // 更新数据单击事件
92 class UpdateListener implements OnClickListener {
93 @Override
94 public void onClick(View v) {
95 System.out.println("update");
96 DatabaseHelper dbHelper = new DatabaseHelper(
97 SQLiteOperationActivity.this, "sqlite_test_db");
98 SQLiteDatabase sqlDb = dbHelper.getWritableDatabase();
99 ContentValues values=new ContentValues();
100 values.put("name", "zhangsanfeng");
101 sqlDb.update("users", values, "id=?", new String[]{"1"});
102 }
103 }
104
105 // 查询数据单击事件
106 class QueryListener implements OnClickListener {
107 @Override
108 public void onClick(View v) {
109 System.out.println("query");
110 DatabaseHelper dbHelper = new DatabaseHelper(
111 SQLiteOperationActivity.this, "sqlite_test_db");
112 SQLiteDatabase sqlDb = dbHelper.getReadableDatabase();
113 Cursor cursor=sqlDb.query("users", new String[]{"name"}, null, null, null, null, null);
114 while(cursor.moveToNext()){
115 String name=cursor.getString(cursor.getColumnIndex("name"));
116 System.out.println(name);
117 }
118 }
119 }
120
121 // 删除数据单击事件
122 class DeleteListener implements OnClickListener {
123 @Override
124 public void onClick(View v) {
125 System.out.println("delete a data");
126 DatabaseHelper dbHelper = new DatabaseHelper(
127 SQLiteOperationActivity.this, "sqlite_test_db");
128 SQLiteDatabase sqlDb = dbHelper.getWritableDatabase();
129 sqlDb.delete("users", "id=?", new String[]{"1"});
130 }
131 }
132
133 //删除表
134 class DropTableListener implements OnClickListener{
135
136 @Override
137 public void onClick(View v) {
138 // TODO Auto-generated method stub
139 System.out.println("drop a table");
140 DatabaseHelper dbHelper = new DatabaseHelper(
141 SQLiteOperationActivity.this, "sqlite_test_db");
142 SQLiteDatabase sqlDb = dbHelper.getWritableDatabase();
143 sqlDb.execSQL("drop table users");
144 }
145
146 }
147 }