简体   繁体   中英

Upgrading database using my OpenHelper in greendao

I use GreenDao as my ORM. I want migrate schema from oldversion to new version. I use this link to implement my mygration. So I wrote my own OpenHelper class and put it to another package. I Implement onUpgrade method like this:

public class UpgradeHelper extends OpenHelper {

public UpgradeHelper(Context context, String name, CursorFactory factory) {
    super(context, name, factory);
}

/**
 * Apply the appropriate migrations to update the database.
 */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i("greenDAO", "My Upgrade Helper -------- Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
    switch (newVersion) {
    case 2:
        new MigrateV1ToV2().applyMigration(db, oldVersion);
        break;
    case 3:
        new MigrateV2ToV3().applyMigration(db, oldVersion);
        break;
    default:
        return;
    }
}

}

But this method never called when I upgrade the version of database from 1 to 2. Also I can not change onUpgrade() Method in generated DaoMaster class, because it is Auto generated. When I upgrade SCHEMA_VERSION, onUpgrade() method is called but it is in DaoMaster class and I can not modify it.

The onUpgrade(Database, oldSchemaVersion, newSchemaVersion) -method is only called if the SCHEMA-VERSION stored in the SQLite-database differs from the SCHEMA-VERSION you specify in your code.

Thus this method will run only once for each version of your database. If you forgot to include your update-logic in the first run, you will have to reset the schema-version manually for example using SQLiteManager .

将SCHEMA_VERSION更改为高于DAOMaster中存在的先前值的值然后将调用onUpgrade方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM