安卓:升级DB版本,并增加新的表 [英] Android: upgrading DB version and adding new table

查看:98
本文介绍了安卓:升级DB版本,并增加新的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建sqlite的表我的应用程序,但现在我想一个新的表添加到数据库中。

I've already created sqlite tables for my app, but now I want to add a new table to the database.

我改变了DB版本低于

private static final int DATABASE_VERSION = 2;

和添加字符串创建表

private static final String DATABASE_CREATE_color = 
   "CREATE TABLE IF NOT EXISTS files(color text, incident_id text)";

的onCreate onUpgrade 如下:

@Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE_incident);
        database.execSQL(DATABASE_CREATE_audio);
        database.execSQL(DATABASE_CREATE_video);
        database.execSQL(DATABASE_CREATE_image);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //drop table and add new tables when version 2 released.
        db.execSQL(DATABASE_CREATE_color);

    }

但由于某些原因,新表未被创建。我究竟做错了什么?

But for some reason the new table is not being created. What am I doing wrong?

推荐答案

1。关于的onCreate()和onUpgrade()

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

2。递增的DB版本

您需要这样一个构造函数:

You need a constructor like:

MyOpenHelper(Context context) {
   super(context, "dbname", null, 2); // 2 is the database version
}

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

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 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 onCreate which keeps you from having to keep incrementing the database version into the stratosphere as you develop...

这篇关于安卓:升级DB版本,并增加新的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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