4

I have an app in Google Play store and I'm working on new version of it. There will be change in SQLite database scheme (new tables) + new ContentProviders.

I consider how to properly solve upgrade of database structure so that users when upgrading my app doesn't lose their current content and also will have created new tables.

Should I change: * private static final int DATABASE_VERSION = 1; to: private static final int DATABASE_VERSION = 2; *

and somehow in onUpgrade() method place code that if detect that oldVersion was 1 and newVersion is 2. Will create new tables.

Now I have some dumb code there where if onUpgrade has been called the database has been destroyed and recreated... but I think that this method has never been called cause DATABASE_VERSION hasn't been changed. And I just uninstall and reinstall app while testing.

What is the best practice?

private static final int DATABASE_VERSION = 1;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
enter code here
} 
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143

1 Answers1

3

Currently I use the following:

private static final int DATABASE_VERSION = 3;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(oldVersion < 2)
         //changes added in DBv2
    } 
    if(oldVersion < 3)
         //changes added in DBv3
    } 
   //so on...
}

This way ensures that changes are made to the database one after another in the correct order

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
  • I have in onUpgrade() calling of SomeProvider.SomeTable.onUpgrade() and I consider whether better to add this checks in each SomeTable.onUpgrade() or directly in SQLiteOpenHelper. onUpgrade() – Michał Ziobro Mar 30 '15 at 14:40