2014-02-18 22 views
0

數據庫幫助與我的想法升級: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,數據庫架構也會不管用戶以前所使用的版本更新,一步一步到最新版本。如果oldVersion8,則將運行升級方法upgradeToNinth(db),upgradeToTenth(db)upgradeToEleventh(db)。大!

但是,此代碼假定newVersion的值始終是數據庫的最新版本(提供給SQLiteOpenHelper構造函數的值)。這是不是真的? onUpgrade方法被稱爲newVersion其他最新的?

回答

1

但是,此代碼假設newVersion的值始終是數據庫的最新版本(提供給SQLiteOpenHelper構造函數的值)。這是不是真的?

是。

這可以通過讀取source來驗證,其中onUpgrade()被調用,newVersion參數與您作爲SQLiteOpenHelper構造函數的參數傳入的參數相同。

0

有沒有情況下onUpgrade方法與newVersion和其他最新的?

onUpgrade只有在您發佈應用程序的更新版本並增加數據庫版本後才能執行。