10

I created an Android application which, check at startup if there is a new version of application. If yes, the application download the new apk file and over-install new apk. My application use the sqlite db. But this db, from one version to other can change. I think I have to use the method:

 onUpgrade()

but I don't know exactly how to use it.

When I start the application I use this code for crete database(if not exists):

DbHelper mDHelper = new DbHelper(context, DB_NAME, null, DB_VERSION)

What should I change if I want use onUpgrade() method? And when do I have to call it?

Kara
  • 6,115
  • 16
  • 50
  • 57
GVillani82
  • 17,196
  • 30
  • 105
  • 172

3 Answers3

20

onUpgrade() is called (you do not call it yourself) when version of your DB changed which means underlying table structure changed etc.

In general it means that OS is telling you "hey, you asked for database structure version 10 but I found we got something older here, so this is you chance to fix that before you start using database (and potentially crash due to structure mismatch)".

In that method you should do all that is necessary to, well.. upgrade structure of your old database to structure matching current version requirements like adding/droping columns, converting row contents or even dropping old db completely and create it from scratch - for Android it does not matter what you do here - it's just a sort of emergency callback for your code to do the necessary job (if any). You need to be aware that users may not update frequently so you have to always handle upgrade from version X to Y knowing that X may not be equal to i.e. (Y-1).

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    I notice that onUpgrade take two int parameters: oldVersion and newVersion. So if I have in my app, database at version 1 and the downloaded apk contains a database at version 2 , this allows the system to call automatically onUpgrade() method? – GVillani82 Oct 31 '12 at 13:40
  • Like suggested in http://code.google.com/p/openintents/source/browse/trunk/notepad/NotePad/src/org/openintents/notepad/NotePadProvider.java?r=3878 I can use switch(oldVersion) for take into account the different changes to make for each old version? is it correct? Furthermore, If,in a new db version, I add new column into some table, I have to modify also the onCreate, in order to create the table with the new additional column? This, probably make sense if no application is yet installed and new db has to be created. Is my assumption correct? – GVillani82 Oct 31 '12 at 13:44
  • 3
    yes. version number is just `int`, so you may do regular `switch/case`. What's important though, is that you shall for example handle upgrade from version 2 to 3 but also from 1 to 3 (I'd probably just loop there and first do upgrade from 1 to 2, then from 2 to 3), which would probably be simpler than handling direct update from 1 to 3 (or from 1 to 80 to give wider perspective) – Marcin Orlowski Oct 31 '12 at 13:47
5

if your are using the SQLiteOpenHelper the onUpgrade will be called whenever you change the DB version. There is an additional requirement for this to work. The db name has to remain the same.

Old Version:
dbName = "mydb.db"
dbVersion = 1

New Version:
dbName = "mydb.db"
dbVersion = 2

in the onCreate of the content provider you create an instance of the SQLiteOpenHelper that takes these params. Your SQLiteOpenHelper implementation would look like this:

public static final class MySQLiteOpenHelper extends SQLiteOpenHelper {

        public MySQLiteOpenHelper(Context context, int dbVersion, String dbName) {
            super(context, dbName, null, dbVersion);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //Code to create your db here
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Code to upgrade your db here
        }

}

it will be also called when you alter you table or add more tables in your database

Just Variable
  • 892
  • 10
  • 19
3

I found this very helpful https://thebhwgroup.com/blog/how-android-sqlite-onupgrade

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) {
         db.execSQL(DATABASE_ALTER_TEAM_1);
    }
    if (oldVersion < 3) {
         db.execSQL(DATABASE_ALTER_TEAM_2);
    }
}
Mr. T
  • 51
  • 4