Android:升级数据库版本并添加新表 [英] Android: upgrading DB version and adding new table

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

问题描述

我已经为我的应用程序创建了 sqlite 表,但现在我想向数据库添加一个新表.

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

我更改了数据库版本如下

I changed the DB version as below

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

onCreateonUpgrade 如下:

@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.增加数据库版本

你需要一个像这样的构造函数:

You need a constructor like:

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

重要提示:仅增加应用版本不足以调用 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 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...

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

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