Skip to content Skip to sidebar Skip to footer

Upgrade Rows On App Upgrade Only

If I want to update DB rows only once only when my users upgrade their app, then would you do it in the OnUpgrade method in my DB handler class or would you do it as an asyncTask

Solution 1:

The typical design pattern for database updates in an app goes something like the code below and every time you update your application where a database change is required, you bump the database version used in your SQLiteOpenHelper-derived class.

This, of course, presumes you used SQLiteOpenHelper to manage getting a reference to your SQLite DB in your provider:

publicvoid onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion == 1) {
                // DO WORK TO UPGRADE FROM VERSION 1 to 2
                oldVersion += 1;
    }

    if (oldVersion == 2) {
                // DO WORK TO UPGRADE FROM VERSION 2 to 3
                oldVersion += 1;
    }

    if (oldVersion == 3) {
                // DO WORK TO UPGRADE FROM VERSION 3 to 4
                oldVersion += 1;
    }
}

This allows any user to upgrade from any previous version to the current version and ensures all the changes are made in the right order. So, it would only be done once, because on the next version, the database version becomes higher.

Post a Comment for "Upgrade Rows On App Upgrade Only"