How to perform a lightweight migration on sqlite database in Android just the way like iOS?

StackOverflow https://stackoverflow.com/questions/18985459

質問

I want to add some keys, such as "URL", to the existed table.

In iOS, I can create a new version of data model and perform a light weight migration.

However, the default way in SQLiteOpenHelper seems to destroy the old DB and create a new one.

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_1 + ", "
            + TABLE_2 + ", " + TABLE_3 + ", "
            + TABLE_4);
    // Create tables again
    onCreate(db);
}

How can I keep the old database and just add some new keys in Android?

役に立ちましたか?

解決

You can write your own Upgrade sql query to upgrade your database.

Call onCreate after droping all tables is just a easy way to upgrade tables without caring about FKs, null fields or database version.

Use oldVersion and newVersion to make the correct changes (alter table) on your database.

onUpgrade doc

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top