SQLite onUpgrade()

别等时光非礼了梦想. 提交于 2020-01-10 15:53:07

问题


i have a problem in the SQLite database in android.

In my onCreate(SQLiteDatabase db) method of SQLiteOpenHelper class, i am creating tables. Now i have this application uploaded at the playstore. Now i figured that i need one more coloumn in those tables.

If i put an update of the application in the playstore (with new queries in onCreate() method), next time the OnUpgrade() method will be called and hence table wont be created again.

so please tell me is there any way to delete the SQLite database when the application is reinstalled or updated.. or delete the whole application before re-installation ?


回答1:


1. About onCreate() and onUpdate()

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

You need a constructor like:

2. Incrementing the db version

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

IMPORTANT: Incrementing the app version alone is not enough for onUpgrade to be called.

3. Don't forget your new users!

Don't forget to add

database.execSQL(DATABASE_CREATE_color);

to your onCreate() method as well or newly installed apps will lack the table.

4. How to deal with multiple database changes over time

When you have successive app upgrades, several of which have database upgrades, you want to be sure to check the 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); 
   }
}

This way 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 revision 3 update... After all, you can't count on 100% of your user base to upgrade each time you release an update. Sometimes they skip an update or 12 :)

Hopefully this makes sense.

5. Keeping your revision numbers under control while developing

And finally... calling

adb uninstall <yourpackagename>

totally uninstalls the app. When you install again, you are guaranteed to hit the onCreate method which keeps you from having to keep incrementing the database version into the stratosphere...




回答2:


As per my understanding you have to add just one column.
You may try with

Alter table

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




回答3:


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




回答4:


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

If they are not there, you get an exception, and then I drop / recreate the tables. The next time the app runs the columns are there, so the select works fine. Not sure its ideal, but it works,



来源:https://stackoverflow.com/questions/14579175/sqlite-onupgrade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!