How can I handle upgrades of the database in later versions gracefully?
So I have my app almost ready to go. I have just been wondering about when I update it how the upgrading of the database will work.
Basically it is a quiz and I will be adding and removing questions and the users personal data needs to be kept for instance. How many correct and incorrect answers and for which questions etc.
This gets a bit more complex when you think I may want to remove or add new questions, 开发者_如何转开发and even worse when changing structure.
But do I have to worry about this code now? I am thinking that the second release of my app could contain the update code. This would allow me to release the initial app earlier and worry about the upgrade in the background.
Is this possible? Or must you have the upgrade code as it were on standby?
You should use an SQLiteOpenHelper
now and you can worry about the onUpgrade()
-code later.
The method is executed if you increase the version-number of the database. You can handle/manage your updates there.
The initial version-number is passed to the constructor.
You can also get/set it by using the corresponding methods of the SQLiteDatabase
-object (which is passed to the onUpgrade()
-method): setVersion()
and getVersion()
Basically SQLiteOpenHelper.onUpgrade()
purpose is to give programmer possibility to make changes into database when version-number of your database is changed. In most cases it can be related to change of database/table scheme/structure. Nevertheless nothing wrong if you will handle with SQLiteOpenHelper.onUpgrade()
also database content change.
In my case, I have chosen another approach, since DB content instantly changed by users, I have in resources XML file (kinda templates.xml) which contains basic information about available primary data (as in your case quizzes) and during first opening of database I'm just uploading primary data to DB. So you can do the same
精彩评论