一、代码
1.xml
(1)activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 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 /> 13 14 <Button 15 android:id="@+id/updateDatabase" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:text="updateDatabase" 19 /> 20 21 <Button 22 android:id="@+id/insert" 23 android:layout_width="fill_parent" 24 android:layout_height="wrap_content" 25 android:text="insert"/> 26 <Button 27 android:id="@+id/update" 28 android:layout_width="fill_parent" 29 android:layout_height="wrap_content" 30 android:text="update"/> 31 <Button 32 android:id="@+id/query" 33 android:layout_width="fill_parent" 34 android:layout_height="wrap_content" 35 android:text="query"/> 36 37 </LinearLayout>
2.java
(1)MainActivity.java
1 package com.example.s01_original_e16_sqlite; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.database.sqlite.SQLiteOpenHelper; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.View; 11 import android.view.View.OnClickListener; 12 import android.widget.Button; 13 14 public class MainActivity extends Activity { 15 16 /** Called when the activity is first created. */ 17 private Button createButton; 18 private Button insertButton; 19 private Button updateButton; 20 private Button updateRecordButton; 21 private Button queryButton; 22 @Override 23 public void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 createButton = (Button)findViewById(R.id.createDatabase); 27 updateButton = (Button)findViewById(R.id.updateDatabase); 28 insertButton = (Button)findViewById(R.id.insert); 29 updateRecordButton = (Button)findViewById(R.id.update); 30 queryButton = (Button)findViewById(R.id.query); 31 createButton.setOnClickListener(new CreateListener()); 32 updateButton.setOnClickListener(new UpdateListener()); 33 insertButton.setOnClickListener(new InsertListener()); 34 updateRecordButton.setOnClickListener(new UpdateRecordListener()); 35 queryButton.setOnClickListener(new QueryListener()); 36 } 37 class CreateListener implements OnClickListener{ 38 @Override 39 public void onClick(View v) { 40 //创建一个DatabaseHelper对象 41 DBHelper dbHelper = new DBHelper(MainActivity.this, "my_sqlite_db"); 42 //只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库 43 SQLiteDatabase db = dbHelper.getReadableDatabase(); 44 } 45 } 46 class UpdateListener implements OnClickListener{ 47 48 @Override 49 public void onClick(View v) { 50 DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2); 51 SQLiteDatabase db = dbHelper.getReadableDatabase(); 52 } 53 54 } 55 class InsertListener implements OnClickListener{ 56 57 @Override 58 public void onClick(View v) { 59 //生成ContentValues对象 60 ContentValues values = new ContentValues(); 61 //想该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据库当中的数据类型一致 62 values.put("id", 1); 63 values.put("name","zhangsan"); 64 DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2); 65 SQLiteDatabase db = dbHelper.getWritableDatabase(); 66 //调用insert方法,就可以将数据插入到数据库当中 67 db.insert("user", null, values); 68 } 69 } 70 //更新操作就相当于执行SQL语句当中的update语句 71 //UPDATE table_name SET XXCOL=XXX WHERE XXCOL=XX... 72 class UpdateRecordListener implements OnClickListener{ 73 74 @Override 75 public void onClick(View arg0) { 76 // TODO Auto-generated method stub 77 //得到一个可写的SQLiteDatabase对象 78 DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2); 79 SQLiteDatabase db = dbHelper.getWritableDatabase(); 80 ContentValues values = new ContentValues(); 81 values.put("name", "zhangsanfeng"); 82 //第一个参数是要更新的表名 83 //第二个参数是一个ContentValeus对象 84 //第三个参数是where子句 85 db.update("user", values, "id=?", new String[]{"1"}); 86 } 87 } 88 class QueryListener implements OnClickListener{ 89 90 @Override 91 public void onClick(View v) { 92 System.out.println("aaa------------------"); 93 Log.d("myDebug", "myFirstDebugMsg"); 94 95 DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2); 96 SQLiteDatabase db = dbHelper.getReadableDatabase(); 97 Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null); 98 while(cursor.moveToNext()){ 99 String name = cursor.getString(cursor.getColumnIndex("name")); 100 System.out.println("query--->" + name); 101 } 102 } 103 } 104 105 }
(2)DBHelper.java
1 package com.example.s01_original_e16_sqlite; 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 DBHelper extends SQLiteOpenHelper { 9 10 private static final int VERSION = 1; 11 //在SQLiteOepnHelper的子类当中,必须有该构造函数 12 public DBHelper(Context context, String name, CursorFactory factory, 13 int version) { 14 //必须通过super调用父类当中的构造函数 15 super(context, name, factory, version); 16 // TODO Auto-generated constructor stub 17 } 18 public DBHelper(Context context,String name){ 19 this(context,name,VERSION); 20 } 21 public DBHelper(Context context,String name,int version){ 22 this(context, name,null,version); 23 } 24 25 //该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabse对象的时候,才会调用这个方法 26 @Override 27 public void onCreate(SQLiteDatabase db) { 28 // TODO Auto-generated method stub 29 System.out.println("create a Database"); 30 //execSQL函数用于执行SQL语句 31 db.execSQL("create table user(id int,name varchar(20))"); 32 } 33 34 @Override 35 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 36 // TODO Auto-generated method stub 37 System.out.println("update a Database"); 38 } 39 40 }