SQLite的onUpgrade() [英] SQLite onUpgrade()

查看:195
本文介绍了SQLite的onUpgrade()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Android上的SQLite数据库的一个问题。

i have a problem in the SQLite database in android.

在SQLiteOpenHelper类的我的onCreate(SQLiteDatabase DB)的方法,我创建表。现在我有这个应用程序在playstore上传。现在我想通了,我需要这些表中多了一个coloumn。

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.

如果我把在playstore应用程序的更新(在新的onCreate查询()方法),下一次OnUpgrade()方法将被调用,因此表不会重新创建。

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.

所以请告诉我,有没有办法删除SQLite数据库应用程序时重新安装或更新..或重新安装之前删除整个应用程序?

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。关于的onCreate()和OnUpdate中()

的onCreate(..)每当应用程序是新安装的被调用。 onUpgrade时,要调用的应用程序进行升级,并推出了和数据库的版本是不一样的。

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。递增的DB版本

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

重要提示:递增单独的应用程序的版本是不够的onUpgrade被称为

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

3。不要忘记你的新用户!

不要忘了添加

database.execSQL(DATABASE_CREATE_color);

您的onCreate()方法,以及或新安装的应用程序将缺乏表。

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

4。如何处理多个数据库随时间的变化

当你有连续的应用程序的升级,其中一些具有数据库的升级,你要确保检查oldVersion:

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); 
   }
}

当用户升级从版本1到版本3,他们得到两个更新这种方式。当用户升级从版本2〜3个,他们只得到了修订3更新......毕竟,你不能指望你的用户群在每次发布一个更新时间提升100%。有时候,他们跳过更新或12:)

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 :)

希望这是有道理的。

5。保持在控制之下,而开发的版本号

最后...调用

adb uninstall <yourpackagename>

完全卸载该应用程序。当您再次安装,保证您打的onCreate方法,让你不必不断递增数据库版本进入平流层...

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

这篇关于SQLite的onUpgrade()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆