SQLite onUpgrade ()

I have a problem with a SQLite database in android.

In my onCreate (SQLiteDatabase db) method of the SQLiteOpenHelper class, I create tables. Now I have this application uploaded to the play store. Now I realized that I need one more coloumn in these tables.

If I put the app update in the playstore (with new requests in the onCreate () method), the next time the OnUpgrade () method will be called and therefore the table will not be created again.

please tell me if there is a way to delete the SQLite database when reinstalling or updating the app, or uninstalling the entire app before reinstalling?

+3


source to share


4 answers


1.About onCreate () and onUpdate ()

onCreate (..) is called whenever the app is just installed. onUpgrade is called whenever the app is upgraded and started and the database version is not the same.

You need a constructor like:

2. Increasing the db version

MyOpenHelper(Context context) {
super(context, "dbname", null, 2);
}

      

IMPORTANT: In order to call onUpgrade, it is not enough to increase the version of the application.

3. Don't forget new users!

Don't forget to add

database.execSQL(DATABASE_CREATE_color);

      

your onCreate () method as well as newly installed apps will be missing a table.



4. How to deal with multiple database changes over time

When you have successive application updates, some of which have database updates, you should definitely check oldVersion:

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   switch(oldVersion) {
   case 1:
       db.execSQL(DATABASE_CREATE_color);
       // we want both updates, so no break statement here...
   case 2:
       db.execSQL(DATABASE_CREATE_someothertable); 
   }
}

      

So when a user upgrades from version 1 to version 3, they get both updates. When a user upgrades from version 2 to 3, they just get the version 3 update ... After all, you can't count on 100% of your user base to update with every update release. Sometimes they miss an update or 12 :)

Hope this makes sense.

5. Keeping revision numbers under control during development

Finally ... the challenge

adb uninstall <yourpackagename>

      

completely uninstalls the application. When you install again, you are guaranteed to be able to use the onCreate method, due to which you do not need to increment the database version in the stratosphere ...

+13


source


As per my understanding, you only need to add one column.
You can try with

Alter table

      



rEF- http://www.sqlite.org/lang_altertable.html

+2


source


onCreate (SQLiteDatabase db) is only called when the database is created for the first time. If you want to handle an upgrade to an existing table, you must use onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) . Concretely handle migration paths from oldVersion to newVersion

0


source


This may not be the best solution, but I have built my code to compare table schemas by selecting the columns I am looking for.

If they are not there, you get an exception and then change / recreate the tables. The next time the app launches the columns, so the selection works fine. Not sure about its ideal, but it works,

0


source







All Articles