Android 資料庫綜述(一) 資料庫片的升級與資料的遷移操作

趙子龍發表於2019-02-27

Android 資料庫綜述(一) 資料庫片的升級與資料的遷移操作

www.studyyoun.com/study_andro…

  • SQLiteOpenHelper 是 Android平臺提供給我們一個資料庫輔助類來建立或開啟資料庫
  • onCreate(SQLiteDatabase db) : 當資料庫被首次建立時執行該方法,一般將建立表等初始化操作在該方法中執行。
  • onUpgrade(SQLiteDatabse dv, int oldVersion,int new Version):當開啟資料庫時傳入的版本號與當前的版本號不同時會呼叫該方法。

  • 除了上述兩個必須要實現的方法外,還可以選擇性地實現onOpen 方法,該方法會在每次開啟資料庫時被呼叫。

public class SqlDBHelper extends SQLiteOpenHelper {

    //當前資料庫版本號 
    private static final int DB_VERSION = 1;
    //當前資料庫的名稱
    private static final String DB_NAME = "wisdom.db";
    //資料庫中的一系列的表 
    public static final String TABLE_NAME_ART = "t_art";
      ... ... ...

    /**
     * 私有的建構函式,只能自己使用,防止繞過同步方法生成多個例項,
     *
     * @param context
     */
    private SqlDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    /*私有的靜態物件,為整個應用程式提供一個sqlite操作的靜態例項,
     * 並保證只能通過下面的靜態方法getHelper(Context context)獲得,
     * 防止使用時繞過同步方法改變它*/
    private static SqlDBHelper instance;//這裡主要解決死鎖問題,是static就能解決死鎖問題

    /**
     * 為應用程式提供一個單一的入口,保證應用程式使用同一個物件運算元據庫,不會因為物件不同而使同步方法失效
     * 其實就是獲取資料庫操作的例項
     * @param context 上下文
     * @return instance
     */
    public static SqlDBHelper getHelper(Context context) {
        if (instance == null)
            instance = new SqlDBHelper(context);
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // 構建建立表的sql 
        String sql = "create table if not exists " + TABLE_NAME_ART + " (Id integer primary key autoincrement,art_id integer, art_content text,art_creat_time varchar(50))";
        //執行建立
        sqLiteDatabase.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        //構建刪除表的SQL 
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME_ART;
        //執行操作
        sqLiteDatabase.execSQL(sql);

        onCreate(sqLiteDatabase);
    }
}複製程式碼

在上述過程中,當升級資料庫的版本的時候,(改變資料庫的版本號就可促發升級操作)onUpgrade方法將會被呼叫,一般在這裡執行升級策略的想關資料快取操作,在這裡執行的是將原來的表直接刪除,然後再建立新的表,當然表中的資料也自然而然的被清除到了
當然這是一種比較暴力而簡單的事,也可以採用比較優雅的方式來進行操作 (如下 )

Android SQLite中優雅的方式進行資料庫的升級與資料遷移

SQLite提供了ALTER TABLE命令,允許使用者重新命名或新增新的欄位到已有表中,但是不能從表中刪除欄位。
並且只能在表的末尾新增欄位,比如,為 t_art_list表中新增兩個欄位:
1 ALTER TABLE t_art_list ADD COLUMN Activation BLOB;
2 ALTER TABLE t_art_list ADD COLUMN Key BLOB;

所有在進行資料庫升級時,涉及到表結構的複雜變動,我們可以採用將原有的表修改(修改一個名稱)為臨時表,然後建立一個新的表,然後將臨時表的資料複製到新的表中,最後把臨時表刪除即可。

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    //oldVersion 資料庫舊版本
    //newVersion 資料庫新版本
    //這裡涉及到不同的版本升級
    if(oldVersion==1){

    }else if(oldVersion==2){

    } ....
    else{

    }
}複製程式碼

升級過程的操作,流程一至,只不過是操作的資料庫相關表不一至

try {

    // 開始
    mLiteDatabase.beginTransaction();

    //1. 將表名改為臨時表
     String alertTable = "ALTER TABLE t_art_list RENAME TO t_art_list_temp";
     db.execSQL(alertTable);

    //2. 建立新表
    String clertNewTableSql = "create table if not exists  t_art_list (Id integer primary key autoincrement,art_id integer, art_content text,art_flag varchar(50))"
      
    db.execSQL(clertNewTableSql);

    //3. 匯入資料  
    String insertSql ="INSERT INTO t_art_list SELECT art_id, art_content,\“\”  FROM t_art_list_temp";
      或者  
    String insertSql =" INSERT INTO t_art_list()SELECT art_id, art_content,\“\”  FROM t_art_list_temp";
    db.execSQL(insertSql);
      * 注意 雙引號”” 是用來補充原來不存在的資料的
      
    //4. 刪除臨時表  
    String deleteSql = "DROP TABLE t_art_list_temp";
    db.execSQL(deleteSql);

    // 成功
    mLiteDatabase.setTransactionSuccessful();

} catch (InterruptedException e) {
    e.printStackTrace();
    //處理回gun 
} finally {
    mLiteDatabase.endTransaction();
}複製程式碼

相關文章