Confusion: How does SQLiteOpenHelper onUpgrade() behave? And together with import of an old database backup?

后端 未结 4 1317
后悔当初
后悔当初 2020-11-30 01:03

let\'s assume I have a database table test_table with 2 columns and a corresponding create script in the SQLiteOpenHelper:

DB_VERSION = 1:
public void onCrea         


        
4条回答
  •  温柔的废话
    2020-11-30 01:17

    below psuedo code shows increamental upgrade

    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            switch(oldVersion) {
    
            case 2:
                    //upgrade logic from version 2 to 3
            case 3:
                    //upgrade logic from version 3 to 4
            case 4:
                    //upgrade logic from version 4 to 5
                    break;
            default:
                    throw new IllegalStateException(
                    "onUpgrade() with unknown oldVersion" + oldVersion));
            }
        }
    

    By incremental upgrade i mean - Notice the missing break statement in case 2 and 3

    say if the old version is 2 and new version is 4, then the logic will upgrade the database from 2 to 3 and then to 4

    if old version is 3 and new version is 4, it will just run the upgrade logic for 3 to 4

    keep adding new cases for every new database version upgrade that will do the increamental changes

提交回复
热议问题