標籤:android sqliteopenhelper sqlite 資料庫 sql
上一篇文章中,簡單介紹了一下android資料庫的一些基本概念,那麼從本節開始,就實戰一下Android資料庫的建立和升級。
上文中,也介紹了,SQLiteOpenHelper是一個抽象類別,是我們用來建立和升級資料庫的最佳實務。下面直接以代碼方式,示範一下資料庫的建立操作。
<span style="font-size:18px;">package com.happy.db.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;/** * @Description: 資料庫輔助類 主要完成資料庫的建立以及升級 */public class MySQLiteOpenHelper extends SQLiteOpenHelper {private static final String DB_NAME = "test.db";private static final String TABLE_NAME = "user";private static final int DB_VERSION = 1;private static final String ID = "id";private static final String NAME = "name";private static final String GENDER = "gender";private static final String TELEPHONE = "telphone";// 建立資料庫的sql語句private static final String CREATE_DB_SQL = "CREATE TABLE " + TABLE_NAME+ "(" + ID + " integer primary key autoincrement , " + NAME+ " text not null ," + GENDER + " text ," + TELEPHONE + " integer "+ ")";/** * 構造方法 * * @param context * 上下文 * @param name * 資料庫名 * @param factory * 遊標工廠 * @param version * 資料庫版本號碼 */public MySQLiteOpenHelper(Context context, String name,CursorFactory factory, int version) {super(context, DB_NAME, null, version);}/** * 當磁碟上不存在資料庫檔案時,輔助類需要建立一個新資料時使用 */@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(CREATE_DB_SQL);}/** * 已經存在資料庫,但是資料庫版本不一樣時調用(升級) */@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}}</span> 如果我們直接運行我們的程式,會發現並沒有把我們的資料庫給建立出來。如果建立和訪問資料庫需要調用getReadableDatabase或getWritableDatabase來擷取資料庫唯讀或可寫的執行個體。當我們調用執行完helper.getReadableDatabase();或helper.getWritableDatabase();後,資料庫執行個體就被建立出來了。
接著使用我們的SQLite Expert Professional 3開啟次資料庫,會看到資料結構如下:
這樣我們便完成了資料庫執行個體的建立。
下面,同樣的方法介紹一下我們資料庫版本的升級。
首先,定義我們升級資料庫的sql語句,在這為user增加一列,並在upgrade中執行該sql。
<span style="font-size:18px;">//升級資料庫語句private static final String UPGRADE_SQL = "ALTER TABLE " + TABLE_NAME+ " ADD COLUMN " + " money float ";/** * 已經存在資料庫,但是資料庫版本不一樣時調用(升級) */@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL(UPGRADE_SQL);}</span>然後在我們的程式中執行如下代碼:
<span style="font-size:18px;">case R.id.bt_upgrade_db://升級資料庫helper = new MySQLiteOpenHelper(getApplicationContext(), "test.db", null, 2);helper.getReadableDatabase();break;</span>
這樣,運行之後,便會將我們的資料庫進行升級,再次將我們的資料庫開啟,會探索資料結構如下:
如此,便完成了我們資料庫的升級邏輯。當然,在實際開發中,upgrade中不會簡單執行一條的,需要對當前資料版進行判斷,對於不同的資料庫版本執行不同的sql語句。
當然,如果我們希望直接管理資料庫的建立 開啟和版本控制操作,而不是使用sqliteopenhelper,那麼可以使用應用程式Context對象的openOrCreateDataBase方法來建立資料庫本身。
<span style="font-size:18px;">Context context = getApplicationContext();SQLiteDatabase db = context.openOrCreateDatabase("test1.db",Context.MODE_PRIVATE, null);db.execSQL("create table student ( id integer primary key autoincrement , name text,sex text,mobile integer) ");</span>
這樣同樣可以建立出來test1.db資料庫,建立完成後記得使用資料庫的exeSQL來根據執行建立和刪除表的操作。
本文就簡單介紹到這吧,都是挺淺的一些東西,也是我們Android中基本用到的建立和升級資料庫的可選方案了。
Android資料庫之建立和升級資料庫(中)