SQLiteOpenHelper onUpgrade()混乱的Andr​​oid [英] SQLiteOpenHelper onUpgrade() Confusion Android

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

问题描述

我做我有一个数据库,第一个应用程序,我有一个小麻烦了解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.

当应用程序通过Android市场确实数据库知道它的版本号升级?所以,我能增加在code中的版本号,然后将其出口到了市场,当用户启动了升级版的第一次,然后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命令的限制,它可以让添加改名只是没有remove /下降这与表中的休闲完成。

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.

那么,什么是推荐onUpgrade:

  • 的BeginTransaction
  • 运行一表的创建与如果不存在(我们正在做一个升级,因此该表可能不存在,但它将无法修改和删除)
  • 放在一个列表中的现有列名单,其中,字符串>列= DBUtils.GetColumns(DB,表名);
  • 备份表( ALTER TABLE+表名+重命名为temp_+表名
  • 创建新表(最新表创建模式)
  • 获得交叉口新列,从升级后的表采取这个时候的列( columns.retainAll(DBUtils.GetColumns(DB,表名));
  • 在恢复数据(字符串COLS = StringUtils.join(列,);             db.execSQL(的String.Format(                     INSERT INTO%S(%S)从temp_%s选择%S,                     表名,COLS,COLS,表名));
  • 删除备份的表( DROP TABLEtemp_+表名
  • 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()混乱的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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