User3
User3

Reputation: 2535

Upgrading DB in Android

I am aware of how the upgrade method works, I am confused at certain points:

1) At the start my DB version was 1 my app version was 1.

2) Now I upgraded my DB version to 2 and my app version is 2.

3) I want to upgrade DB to 3 and app to 3.

Question: What happens to people who are upgrading from app version 1 to app version 3? Will they get the upgrade of DB to 2 and then to 3? Or do I need to write code for that? How do I maintain such flags?

Upvotes: 2

Views: 89

Answers (2)

injecteer
injecteer

Reputation: 20707

usually, when the DB upgrade is incremental, you could use an elegant switch:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  switch( oldVersion ){
    case 1:
      migrateFrom1();
    case 2:
      migrateFrom2();
    case 3:
      migrateFrom3();
  }
}

notice the absence of break statements.

Upvotes: 0

JHH
JHH

Reputation: 9315

Well, SQLiteOpenHelper.onUpgrade will be called as usual, and the simplest way to handle this is to have your upgrade code handle each version step-wise; from version 1 -> 2 -> 3 etc. For example:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (oldVersion == 1) { // upgrade db to version 2 oldVersion = 2; } if (oldVersion == 2) { // upgrade db to version 3 oldVersion = 3; } }

Upvotes: 2

Related Questions