數據庫幫助與我的想法升級:SQLiteOpenHelper#onUpgrade() - 是newVersion alwyays的最新版本嗎?
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myapp.db";
private static final int DATABASE_VERSION = 11;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) { /* ... */ }
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion) {
case 1: upgradeToSecond(db);
case 2: upgradeToThird();
case 3: upgradeToFourth();
// ...
case 10: upgradeToEleventh();
}
}
private void upgradeToSecond(SQLiteDatabase db) { /* ... */ }
private void upgradeToThird(SQLiteDatabase db) { /* ... */ }
private void upgradeToFourth(SQLiteDatabase db) { /* ... */ }
// ...
private void upgradeToEleventh(SQLiteDatabase db) { /* ... */ }
}
由於開關沒有break
S,數據庫架構也會不管用戶以前所使用的版本更新,一步一步到最新版本。如果oldVersion
爲8
,則將運行升級方法upgradeToNinth(db)
,upgradeToTenth(db)
和upgradeToEleventh(db)
。大!
但是,此代碼假定newVersion
的值始終是數據庫的最新版本(提供給SQLiteOpenHelper
構造函數的值)。這是不是真的? onUpgrade
方法被稱爲newVersion
其他最新的?