數(shù)據(jù)庫的創(chuàng)建與升級
Android中專門提供了SQLiteOpenHelper幫助類,幫助我們創(chuàng)建和升級數(shù)據(jù)庫涧衙。SQLiteOpenHelper是一個抽象類哪工,要想使用它,我們必須創(chuàng)建一個自己的幫助類弧哎。并重寫onCreate()
和onUpgrade()
l兩個抽象方法,從而對它進(jìn)行使用雁比。
SQLiteOpenHelper有兩個構(gòu)造方法:
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory, int version)
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory, int version,DatabaseErrorHandler errorHandler)
/*
第一個參數(shù)為Context
第二個參數(shù)為數(shù)據(jù)庫名,創(chuàng)建數(shù)據(jù)庫時使用此名字
第三個參數(shù)允許我們在查詢數(shù)據(jù)時返回一個自定義Cursor
第四個參數(shù)表示數(shù)據(jù)庫當(dāng)前的版本號撤嫩,用于對數(shù)據(jù)庫進(jìn)行升級
第五個參數(shù)用于處理數(shù)據(jù)庫的異常
*/
SQLiteOpenHelper中的方法:
返回類型 | 方法 |
---|---|
void |
close() 關(guān)閉所有數(shù)據(jù)庫對象(Close any open database object.) |
String |
getDatabaseName() 獲取使用構(gòu)造函數(shù)時傳入的數(shù)據(jù)庫名 |
SQLiteDatabase |
getReadableDatabase() 打開或創(chuàng)建一個只讀的數(shù)據(jù)庫對象 |
SQLiteDatabase |
getWritableDatabase() 打開或創(chuàng)建一個可讀寫的數(shù)據(jù)庫對象 |
abstract void |
onCreate(SQLiteDatabase db) 當(dāng)數(shù)據(jù)庫創(chuàng)建時調(diào)用 |
void |
onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) 當(dāng)數(shù)據(jù)庫降級時調(diào)用 |
void |
onOpen() 當(dāng)數(shù)據(jù)庫打開時調(diào)用 |
abstract void |
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 當(dāng)數(shù)據(jù)庫升級時調(diào)用 |
void |
setWriteAheadLoggingEnabled(boolean enabled) 是否打印預(yù)寫式日志 |
數(shù)據(jù)庫文件通常存放在/data/data/packagename/databases/目錄下
數(shù)據(jù)庫只有在getReadableDatabase()
或者getWritableDatabase()
被調(diào)用時才真正被打開或創(chuàng)建偎捎。
onCreate()
在數(shù)據(jù)庫被創(chuàng)建時調(diào)用,重寫這個方法創(chuàng)建表和表的初始種群
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(SQL_COMMAND);
//SQL_COMMAND為sql語句
}
當(dāng)創(chuàng)建SQLiteOpenHelper對象時序攘,需指定一個version參數(shù)茴她,當(dāng)參數(shù)高于之前的版本號時調(diào)用onUpgrade()
,當(dāng)參數(shù)低于之前的版本號時調(diào)用onDowngrade()
代碼實現(xiàn):
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book("
+ "id integer primary key autoincrement, "
+ "author text, "
+ "price real, "
+ "pages integer, "
+ "name text)";
public static final String CREATE_CATEGORY ="create table Category ("
+"id integer primary key autoincrement, "
+"category_name text, "
+"category_code integer)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
添加數(shù)據(jù)
添加數(shù)據(jù)主要有兩種方法
1.使用execSQL()
方法執(zhí)行SQL語句程奠,此方法適用于所有不返回結(jié)果的SQL語句
String sql = "insert into Book(name,author,pages,price)values(?,?,?,?)",
new String[]{"bookname","zsy","200","5.00"};
db.execSQL(sql);
//
2.使用insert()
方法
ContentValues values = new ContentValues();
values.put("name", "The Da Vinci Code");
values.put("author", "Dan Brown");
values.put("pages", 454);
values.put("price", 16.96);
db.insert("Book", null, values);
insert()
方法接受3個參數(shù)丈牢,第一個是表名,第二個用于在未添加數(shù)據(jù)時給某些可為空的隊列復(fù)制NULL瞄沙,一般用不到此功能己沛,傳入null即可,第三個為ContentValues對象
更新數(shù)據(jù)
1.使用execSQL()
方法
2.使用updata()
方法
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price", 10.99);
db.update("Book", values, "name=?", new String[]{"The Da Vinci Code"});
updata()
接受4個參數(shù)
- 表名
- ContentValues對象距境,要更新的數(shù)據(jù)裝在這里
- 4.約束更新一行或幾行的數(shù)據(jù)泛粹,不指定的話默認(rèn)更新所有行
刪除數(shù)據(jù)
1.使用execSQL()
方法
2.使用delete()
方法
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages > ?", new String[]{"500"});
delete()
接收三個參數(shù)
- 表名
- 3.約束刪除某一行或某幾行的數(shù)據(jù),默認(rèn)刪除所有行
查詢數(shù)據(jù)
1.使用execSQL()
方法
2.使用inquery()
方法
query()方法參數(shù) | 對應(yīng)SQL部分 | 描述 |
---|---|---|
table | from_table_name | 指定查詢的表名 |
columns | select column1, column2 | 指定查詢的列名 |
selection | where column = value | 指定where的拘束條件 |
selectionArgs | - | 為where中的占位符提供具體的值 |
groupBy | group by column | 指定需要group by的列 |
having | having column = value | 對 group by 后的結(jié)果進(jìn)一步約束 |
orderBy | order by column1, column2 | 指定查詢結(jié)果的排序方式 |
代碼示例:
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book",null,null,null,null,null,null);
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
}while(cursor.moveToNext());
}
cursor.close();
不管你如何執(zhí)行查詢肮疗,都會返回一個 Cursor晶姊,這是 Android 的 SQLite 數(shù)據(jù)庫游標(biāo),使用游標(biāo)伪货,你可以:
- 通過使用 getCount() 方法得到結(jié)果集中有多少記錄们衙;
- 通過 moveToFirst(), moveToNext(), 和 isAfterLast() 方法遍歷所有記錄钾怔;
- 通過 getColumnNames() 得到字段名;
- 通過 getColumnIndex() 轉(zhuǎn)換成字段號蒙挑;
- 通過 getString()宗侦,getInt() 等方法得到給定字段當(dāng)前記錄的值;
- 通過 requery() 方法重新執(zhí)行查詢得到游標(biāo)忆蚀;
- 通過 close() 方法釋放游標(biāo)資源矾利;