ORMLite update of the database

前端 未结 4 743
陌清茗
陌清茗 2020-12-09 04:18

I\'m actually developing an app which is using ORMLite library (which is wonderful btw) but I\'m a beginner using it.

I have a question about the update of the datab

4条回答
  •  情书的邮戳
    2020-12-09 04:51

    I do it this way:

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    
        switch (oldVersion) {
        case 1:
            updateFromVersion1(database, connectionSource, oldVersion, newVersion);
            break;
        case 2:
            updateFromVersion2(database, connectionSource, oldVersion, newVersion);
            break;
        default:
            // no updates needed
            break;
        }
    
    }
    
    private void updateFromVersion1(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        // do some stuff here
        onUpgrade(database, connectionSource, oldVersion + 1, newVersion);
    }
    
    private void updateFromVersion2(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        // do some stuff here
        onUpgrade(database, connectionSource, oldVersion + 1, newVersion);
    }
    

    This will incrementally update the users db independent from which db version he is coming.

提交回复
热议问题