How to deal with changes to several versions of the database when updating Android applications

Thanks for reading my question here.

Actually, I have some confusion about how to work with SQLite tables, when I need to add some rows to a table for several versions of sqllite.

I google things and found something like this, we have to do an alter table in the method onUpgrade. This will be a sequential update of the work.

Suppose i have 3 devices they have below versions of database
1) 1st device = database version 1
2) 2nd device = database version 2
3) 3rd device = application not installed.

database version 1 = 2 columns
database version 2 = 2+2 columns
database version 3 = 4+2 columns.

So, if I need to update my Android application users, what should I do? I have the following scripts.

1) Updated application would be install in 3rd device where still application is not installed.
2) Application should be update from version 1 to version 3 (Device 1st).
3) Application should be update from version 2 to version 3 (Device 2nd).

Here I can get an idea of ​​how to handle script number 3, but I do not know how I can handle scenarios of the 1st and 2nd.

How can I write code so that it works for all scenarios.

onUpgrade onCreate .

. .

+4
1

, (, onUpgrade), , SQLiteOpenHelper.

3.

onCreate (SQLiteDatabase db) , 3 (, col1, col2 col3). , v3.

onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion), , , .

if (oldVersion == 1 && newVersion = 2)
{
    //Going from v1 to v2
    db.execSQL("ALTER TABLE mytable ADD COLUMN col2 INTEGER");
}

if (oldVersion == 1 && newVersion = 3)
{
    //Going from v1 to v3
    db.execSQL("ALTER TABLE mytable ADD COLUMN col2 INTEGER");
    db.execSQL("ALTER TABLE mytable ADD COLUMN col3 INTEGER");
}

if (oldVersion == 2 && newVersion = 3)
{
    //Going from v2 to v3
    db.execSQL("ALTER TABLE mytable ADD COLUMN col3 INTEGER");
}

, , . , , , , onCreate. , , , , onUpgrade, , // .

+7

All Articles