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?
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).