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