views:

55

answers:

1

Is the onUpgrade method of SQLiteOpenHelper ever called? If so, when is it called and by what? Or if it is not called by developers, then why is it there? What should be done in the method I have seen examples where it drops all the tables, but then a comment says that dropping all the tables is NOT what you should do.

+3  A: 

Hi,

It is called when you construct a SQLiteOpenHelper with version newer than the version of the opened database. What to do depends on the changes in the database that are made between the old and new versions. The only case when you don't drop a changed table is when the change is noting more than an added column. Then you can use ALTER TABLE statement to add the new column to the table signature.

ognian
The change could also be adding new tables, in which case you might not drop any existing tables.
CommonsWare
But how would yo know ahead of time that you will modify a table? Or do you just have to change the method every time you publish an update.
Mohit Deshpande
You know when you're changing the database and add another case in onUpgrade. So when the user updates the app, the SQLiteOpenHelper knows that the existing database is obsolete and takes the according action. Check out a piece of Android source for reference: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
ognian