SQLite數(shù)據(jù)庫--升級數(shù)據(jù)庫最佳操作

在程序發(fā)布以后,若我們再次開發(fā)升級了新的版本,此時,若數(shù)據(jù)庫也增加了表或者原有的表需要新增字段,在不刪除原數(shù)據(jù)庫的情況下,進行數(shù)據(jù)庫的升級

模擬數(shù)據(jù)庫升級案例

第一版程序,只創(chuàng)建一張Book表

MyDatabaseHelper中的代碼

public class MyDatabaseHelper extends SQLiteOpenHelper{
  //創(chuàng)建Book表的SQL語句
  public static final String CREATE_BOOK = "create table Book("
    + "id integer primary key autoincrement,"
    + "anthor text,"
    + "price real,"
    + "pages integer,"
    + "name text)";
  public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version){
    super(context, name, factory, version);
  }
  @override
  public void onCreate(SQLiteDatabase db){
    //執(zhí)行SQL語句,創(chuàng)建Book表
    db.execSQL(CREATE_BOOK );
  }
  @override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
  }
}

這樣當(dāng)用戶安裝了第一版程序時,其存儲空間上自然就有了Book表

第二版程序,數(shù)據(jù)庫中新增一個Category表

修改MyDatabaseHelper 中的代碼

public class MyDatabaseHelper extends SQLiteOpenHelper{
  //創(chuàng)建Book表的SQL語句
  public static final String CREATE_BOOK = "create table Book("
    + "id integer primary key autoincrement,"
    + "anthor text,"
    + "price real,"
    + "pages integer,"
    + "name text)";

  //創(chuàng)建Category表的SQL語句
  public static final String CREATE_CATEGORY = "create table Category("
  + "id integer primary key autoincrement,"
  + "category_name text,"
   + "category_code integer";

  public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version){
    super(context, name, factory, version);
  }
  @override
  public void onCreate(SQLiteDatabase db){
    //執(zhí)行SQL語句,創(chuàng)建Book表
    db.execSQL(CREATE_BOOK );
    //執(zhí)行SQL語句,創(chuàng)建Category表
    db.execSQL(CREATE_CATEGORY );
  }
  @override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    switch(oldVersion){
      case 1:
        db.execSQL(CREATE_CATEGORY);
      default;
    }
  }
}
  • onCreate()方法中新增了一條創(chuàng)建Category表的語句:當(dāng)用戶沒有使用個過這個程序時,直接安裝了第二版的程序,首次使用的時候就會創(chuàng)建Book表和Category表
  • onUpgrade()方法中添加了一個switch判斷,若用戶使用了第一版的程序,再安裝第二版的程序來覆蓋第一版的程序,就會調(diào)用onUpgrade()方法,當(dāng)舊版本的數(shù)據(jù)庫版本是1,就會執(zhí)行創(chuàng)建Category表的SQL語句
第三版程序,Book表和Category表建立連接

修改MyDatabaseHelper 中的代碼

public class MyDatabaseHelper extends SQLiteOpenHelper{
  //創(chuàng)建Book表的SQL語句
  public static final String CREATE_BOOK = "create table Book("
    + "id integer primary key autoincrement,"
    + "anthor text,"
    + "price real,"
    + "pages integer,"
    + "name text)";

  //創(chuàng)建Category表的SQL語句,較第二版程序比起來,新增了一個字段
  public static final String CREATE_CATEGORY = "create table Category("
  + "id integer primary key autoincrement,"
  + "category_name text,"
   + "category_code integer"
   + "category_id integer";

  public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version){
    super(context, name, factory, version);
  }
  @override
  public void onCreate(SQLiteDatabase db){
    //執(zhí)行SQL語句,創(chuàng)建Book表
    db.execSQL(CREATE_BOOK );
    //執(zhí)行SQL語句,創(chuàng)建Category表
    db.execSQL(CREATE_CATEGORY );
  }
  @override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    switch(oldVersion){
      case 1:
        db.execSQL(CREATE_CATEGORY);
      case 2:
        db.execSQL("alter table Book add column category_id integer");
      default;
    }
  }
}
  • 創(chuàng)建Category表的SQL語句新增了一個字段,當(dāng)用戶沒有使用過本程序時,直接安裝的是第三個版本的程序就會執(zhí)行onCreate()方法,實現(xiàn)了創(chuàng)建兩個數(shù)據(jù)庫表的功能
  • 當(dāng)用戶由第一個版本或者第二個版本升級到第三個版本的時候,就會執(zhí)行onUpgrade()方法,注意沒有break

這樣升級數(shù)據(jù)庫就沒有數(shù)據(jù)的丟失了......

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容