SQLite是Android使用的輕量級的資料庫,開發Android應用是對資料庫的操作自然是必不可少。
Android提供了一個SQLiteOpenHelper類來可以很方便的操作資料庫,
繼承和擴充SQLiteOpenHelper類主要做的工作就是重寫以下兩個方法。
onCreate: 當資料庫被首次建立時執行該方法,一般將建立表等初始化操作在該方法中執行。
onUpgrade:當開啟資料庫時傳入的版本號碼與當前的版本號碼不同時會調用該方法。
下面是我寫的一個SQLite基本操作的demo。
主要包含兩個java類——
DBUtil類,繼承自SQLiteOpenHelper,用以實現各種操作功能:
[java]
package barry.android.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBUtil extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "db2004";
private final static int DATABASE_VERSION = 1;
private static final String TABLE_NAME ="students";
private static final String FILED_1 = "name";
private static final String FILED_2 = "password";
public DBUtil(Context context){
super(context, DATABASE_NAME,null,DATABASE_VERSION);
System.out.println("new DBUtil");
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE "+TABLE_NAME+" ( "+FILED_1 +" TEXT, "+ FILED_2 +" TEXT );";
db.execSQL(sql);
System.out.println("oncreate建立表");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("onUpgrade刪除表");
this.onCreate(db);
}
/**
* 查詢表中所有的資料
* @return
*/
public Cursor select(){
return this.getReadableDatabase()
.query(TABLE_NAME, null, null, null, null, null, null);
}
/**
* 插入一條資料到表中
* @param name 欄位一的值
* @param password 欄位二的值
*/
public void insert(String name ,String password){
ContentValues cv = new ContentValues();
cv.put(FILED_1, name);
cv.put(FILED_2, password);
this.getWritableDatabase().insert(TABLE_NAME, null, cv);
this.getWritableDatabase().close();//關閉資料庫物件
}
/**
* 刪除表中的若干條資料
* @param name 一個包含所有要刪除資料的"name"欄位的數組
*/
public void delete(String[] name){
String where = FILED_1+" = ?";
String[] whereValues = name;
this.getWritableDatabase().delete(TABLE_NAME, where, whereValues);
this.getWritableDatabase().close();
}
/**
* 更新表中的資料(修改欄位二"password")
* @param name 要更新的資料"name"欄位值
* @param newPassword 新的"password"欄位
*/
public void update(String name,String newPassword){
ContentValues cv = new ContentValues();
cv.put(FILED_2, newPassword);
String where =FILED_1+" = ?";
String[] whereValues= {name};
this.getWritableDatabase().update(TABLE_NAME, cv, where, whereValues);
this.getWritableDatabase().close();
}
/**
* 清空表中的資料
*/
public void clean (){
this.getWritableDatabase().execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("clean刪除表");
this.onCreate(this.getWritableDatabase());
this.getWritableDatabase().close();
}
}
package barry.android.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBUtil extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "db2004";
private final static int DATABASE_VERSION = 1;
private static final String TABLE_NAME ="students";
private static final String FILED_1 = "name";
private static final String FILED_2 = "password";
public DBUtil(Context context){
super(context, DATABASE_NAME,null,DATABASE_VERSION);
System.out.println("new DBUtil");
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE "+TABLE_NAME+" ( "+FILED_1 +" TEXT, "+ FILED_2 +" TEXT );";
db.execSQL(sql);
System.out.println("oncreate建立表");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("onUpgrade刪除表");
this.onCreate(db);
}
/**
* 查詢表中所有的資料
* @return
*/
public Cursor select(){
return this.getReadableDatabase()
.query(TABLE_NAME, null, null, null, null, null, null);
}
/**
* 插入一條資料到表中www.2cto.com
* @param name 欄位一的值
* @param password 欄位二的值
*/
public void insert(String name ,String password){
ContentValues cv = new ContentValues();
cv.put(FILED_1, name);
cv.put(FILED_2, password);
this.getWritableDatabase().insert(TABLE_NAME, null, cv);
this.getWritableDatabase().close();//關閉資料庫物件
}
/**
* 刪除表中的若干條資料
* @param name 一個包含所有要刪除資料的"name"欄位的數組
*/
public void delete(String[] name){
String where = FILED_1+" = ?";
String[] whereValues = name;
this.getWritableDatabase().delete(TABLE_NAME, where, whereValues);
this.getWritableDatabase().close();
}
/**
* 更新表中的資料(修改欄位二"password")
* @param name 要更新的資料"name"欄位值
* @param newPassword 新的"password"欄位
*/
public void update(String name,String newPassword){
ContentValues cv = new ContentValues();
cv.put(FILED_2, newPassword);
String where =FILED_1+" = ?";
String[] whereValues= {name};
this.getWritableDatabase().update(TABLE_NAME, cv, where, whereValues);
this.getWritableDatabase().close();
}
/**
* 清空表中的資料
*/
public void clean (){
this.getWritableDatabase().execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("clean刪除表");
this.onCreate(this.getWritableDatabase());
this.getWritableDatabase().close();
}
}
以及調用DBUtil的Activity:
[java]
package barry.android.db;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
public class Demo04_helperActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBUtil dbUtil = new DBUtil(this);
dbUtil.insert("周杰倫", "jaychou");
dbUtil.insert("韓寒", "twocolds");
dbUtil.insert("郭德綱", "yunhejiuxiao");
System.out.println("***********************************全部資料息");
printData(dbUtil);
dbUtil.delete(new String[]{"周杰倫"});
System.out.println("***********************************刪除'周杰倫'之後資料");
printData(dbUtil);
dbUtil.update("郭德綱", "longtengsihai");;
System.out.println("***********************************修改‘郭德綱’的密碼為'longtengsihai'");
printData(dbUtil);
dbUtil.clean();
}
private void printData(DBUtil dbUtil) {
Cursor cursor = dbUtil.select();
if(cursor.moveToFirst()){
System.out.println("當前表中的資料條數:"+cursor.getCount());
do{
System.out.println(cursor.getString(0)+cursor.getString(1));
}while(cursor.moveToNext());
}
cursor.close();
}
}
package barry.android.db;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
public class Demo04_helperActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBUtil dbUtil = new DBUtil(this);
dbUtil.insert("周杰倫", "jaychou");
dbUtil.insert("韓寒", "twocolds");
dbUtil.insert("郭德綱", "yunhejiuxiao");
System.out.println("***********************************全部資料息");
printData(dbUtil);
dbUtil.delete(new String[]{"周杰倫"});
System.out.println("***********************************刪除'周杰倫'之後資料");
printData(dbUtil);
dbUtil.update("郭德綱", "longtengsihai");;
System.out.println("***********************************修改‘郭德綱’的密碼為'longtengsihai'");
printData(dbUtil);
dbUtil.clean();
}
private void printData(DBUtil dbUtil) {
Cursor cursor = dbUtil.select();
if(cursor.moveToFirst()){
System.out.println("當前表中的資料條數:"+cursor.getCount());
do{
System.out.println(cursor.getString(0)+cursor.getString(1));
}while(cursor.moveToNext());
}
cursor.close();
}
}
該程式所執行的操作為:
1.在建立一個名為"db2004"的資料庫,(即DBUtil的“DATABASE_NAME”欄位)。
2.當資料庫被首次建立時執行DBUtil的onCreate方法,建立一張名為students的表,包含兩個欄位(name,password)。(即DBUtil的”TABLE_NAME、FILED_1、FILED_2”欄位)。
3.往資料庫中插入三條資料“周杰倫、韓寒、郭德綱”。然後.查詢出表中所有資料並列印。
4.刪除資料“周杰倫”。然後.查詢出表中所有資料並列印。
5.將資料“郭德綱”的password修改為"longtengsihai"。然後.查詢出表中所有資料並列印。
6.清除表中的所有資料,程式結束。
執行的結果為:
02-07 11:22:47.361: I/System.out(962): new DBUtil
02-07 11:22:47.490: I/System.out(962): ***********************************全部資料息
02-07 11:22:47.490: I/System.out(962): 當前表中的資料條數:3
02-07 11:22:47.500: I/System.out(962): 周杰倫jaychou
02-07 11:22:47.500: I/System.out(962): 韓寒twocolds
02-07 11:22:47.500: I/System.out(962): 郭德綱yunhejiuxiao
02-07 11:22:47.511: I/System.out(962): ***********************************刪除'周杰倫'之後資料
02-07 11:22:47.540: I/System.out(962): 當前表中的資料條數:2
02-07 11:22:47.540: I/System.out(962): 韓寒twocolds
02-07 11:22:47.550: I/System.out(962): 郭德綱yunhejiuxiao
02-07 11:22:47.560: I/System.out(962): ***********************************修改‘郭德綱’的密碼為'longtengsihai'
02-07 11:22:47.590: I/System.out(962): 當前表中的資料條數:2
02-07 11:22:47.590: I/System.out(962): 韓寒twocolds
02-07 11:22:47.590: I/System.out(962): 郭德綱longtengsihai
02-07 11:22:47.601: I/System.out(962): clean刪除表
02-07 11:22:47.610: I/System.out(962): oncreate建立表
結果正確。
摘自 狼的第二個小窩