在程序發(fā)布以后掉缺,若我們再次開發(fā)升級了新的版本眶明,此時筐高,若數(shù)據(jù)庫也增加了表或者原有的表需要新增字段柑土,在不刪除原數(shù)據(jù)庫的情況下,進(jìn)行數(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()
方法丰涉,實(shí)現(xiàn)了創(chuàng)建兩個數(shù)據(jù)庫表的功能 - 當(dāng)用戶由第一個版本或者第二個版本升級到第三個版本的時候,就會執(zhí)行
onUpgrade()
方法斯碌,注意沒有break
這樣升級數(shù)據(jù)庫就沒有數(shù)據(jù)的丟失了......