2

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?

User3
  • 2,465
  • 8
  • 41
  • 84
  • 1
    Are you looking for this: http://stackoverflow.com/questions/20349902/how-to-deal-with-multiple-database-version-changes-when-android-application-upda ? – Javi Mollá Feb 24 '15 at 10:58
  • Here's the solution for your problem. http://stackoverflow.com/questions/8133597/android-upgrading-db-version-and-adding-new-table] – HimanshuGargas Feb 24 '15 at 11:01

2 Answers2

2

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; } }

JHH
  • 8,567
  • 8
  • 47
  • 91
0

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.

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • 1
    Since when are switches elegant LOL? And this particular one with no breaks or returns in each case gives me the chills. – Mister Smith Feb 24 '15 at 12:05
  • I found an elegant way [here](http://stackoverflow.com/questions/20349902/how-to-deal-with-multiple-database-version-changes-when-android-application-upda) – User3 Feb 24 '15 at 12:35
  • @User3 I don't see how that is more elegant, it requires one piece of code for each combination of old and new version. Consider what this would look like in a database at version 50. In my opinion it's better, clearer and less error-prone (because you can actually test all cases easily) to handle upgrades step-wise in deltas. – JHH Feb 25 '15 at 09:09