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