SQLiteOpenHelper onUpgrade() 混淆 Android [英] SQLiteOpenHelper onUpgrade() Confusion Android

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

问题描述

我正在使用数据库开发我的第一个应用程序,但在理解 onUpgrade 函数时遇到了一些麻烦.我的数据库有一个表,其中包含一个项目和一个收藏夹列,以便用户可以收藏一个项目.我看到的大多数实现只是删除表并重建它,但我不想这样做.我希望能够向表格中添加更多项目.

I am doing my first app with a database and I am having a little trouble understanding the onUpgrade function. My database has a table with an items and a favorite column so that the user can favorite an item. Most implementations I see simply drop the table and reconstruct it but I don't want to do this. I want to be able to add more items to the table.

当应用通过安卓市场升级时,数据库知道它的版本号吗?那么我可以增加代码中的版本号,然后将其导出到市场,当用户第一次启动升级版本时,将调用 onUpgrade 吗?

When the app is upgraded through the android marketplace does the database know its version number? So could I increment the version number in the code and then export it to the marketplace and when the user boots up the upgraded version for the first time then onUpgrade will be called?

如果是这种情况,我的 onUpgrade 会简单地从一个文件中提取并添加数据库项.这是一种标准的做事方式还是在 Android 中有更好的处理方式.我尽量保持标准.

If this is the case my onUpgrade would simply pull from a file and add the database items in. Is this a standard way of doing things or is there a better way of handling this in Android. I am trying to stay as standard as possible.

谢谢

推荐答案

好的,在你遇到更大的问题之前,你应该知道 SQLite 对 ALTER TABLE 命令的限制,它允许 addrename 只有没有删除/删除,这是通过重新创建表来完成的.

Ok, before you run into bigger problems you should know that SQLite is limited on the ALTER TABLE command, it allows add and rename only no remove/drop which is done with recreation of the table.

您应该始终手头有新的表创建查询,并将其用于升级和传输任何现有数据.注意:onUpgrade 方法为您的 sqlite 助手对象运行一个,您需要处理其中的所有表.

You should always have the new table creation query at hand, and use that for upgrade and transfer any existing data. Note: that the onUpgrade methods runs one for your sqlite helper object and you need to handle all the tables in it.

那么关于升级的推荐:

  • 开始交易
  • 使用if not exists 运行一个表创建(我们正在进行升级,所以该表可能还不存在,它会失败并删除)
  • 将现有列放入一个列表 List;columns = DBUtils.GetColumns(db, TableName);
  • 备份表 (ALTER table " + TableName + " RENAME TO 'temp_" + TableName)
  • 创建新表(最新的表创建模式)
  • 获取与新列的交集,这次是从升级表中提取的列 (columns.retainAll(DBUtils.GetColumns(db, TableName));)
  • 恢复数据(String cols = StringUtils.join(columns, ",");db.execSQL(String.format("INSERT INTO %s (%s) SELECT %s from temp_%s",TableName, cols, cols, TableName));)
  • 删除备份表(DROP table 'temp_" + TableName)
  • setTransactionSuccessful
  • beginTransaction
  • run a table creation with if not exists (we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)
  • put in a list the existing columns List<String> columns = DBUtils.GetColumns(db, TableName);
  • backup table (ALTER table " + TableName + " RENAME TO 'temp_" + TableName)
  • create new table (the newest table creation schema)
  • get the intersection with the new columns, this time columns taken from the upgraded table (columns.retainAll(DBUtils.GetColumns(db, TableName));)
  • restore data (String cols = StringUtils.join(columns, ","); db.execSQL(String.format( "INSERT INTO %s (%s) SELECT %s from temp_%s", TableName, cols, cols, TableName)); )
  • remove backup table (DROP table 'temp_" + TableName)
  • setTransactionSuccessful

(这不处理表降级,如果重命名列,由于列名不匹配,不会传输现有数据).

.

public static List<String> GetColumns(SQLiteDatabase db, String tableName) {
    List<String> ar = null;
    Cursor c = null;
    try {
        c = db.rawQuery("select * from " + tableName + " limit 1", null);
        if (c != null) {
            ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
        }
    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    } finally {
        if (c != null)
            c.close();
    }
    return ar;
}

public static String join(List<String> list, String delim) {
    StringBuilder buf = new StringBuilder();
    int num = list.size();
    for (int i = 0; i < num; i++) {
        if (i != 0)
            buf.append(delim);
        buf.append((String) list.get(i));
    }
    return buf.toString();
}

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

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