简体   繁体   English

如何在onUpgrade中添加列并将现有行设置为特定值?

[英]How do I add a column in onUpgrade and set existing rows to a particular value?

How do I add a column in DatabaseHelper onUpgrade and set pre-existing rows to a particular value? 如何在DatabaseHelper onUpgrade中添加列并将预先存在的行设置为特定值?

I tried: 我试过了:

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    ...
       if (oldVersion < 2) {
          db.execSQL("ALTER TABLE " + MyTableName + " ADD COLUMN "
              + MyNewColumn + " TEXT;");      
          db.execSQL("UPDATE " + MyTableName  + " SET " +
               MyNewColumn  + "=" + "value for existing;");
       }
    }

But I don't think the alter has been committed yet because it says the column doesn't exist. 但是我不认为alter已经提交了,因为它说该列不存在。 Any suggestion? 有什么建议吗?

Edit: Added some more surrounding code 编辑:添加了一些更多的周围代码

Edit: My mistake. 编辑:我的错误。 I was just missing a quote around the value part and the error about no column threw me off, but the issue was it was looking for the column for the value I had used without the quote. 我只是错过了关于值部分的引用,并且关于没有列的错误让我失望,但问题是它正在寻找我没有引用的值使用的列。

It's not as simple adding a column as you have shown as the onUpgrade will run every time you upgrade the database and adding the column a second time will fail... If you wish to do it quick and dirty you can wrap it in a try catch 添加一个列并不像你所示的那样简单,因为每次升级数据库都会运行onUpgrade,第二次添加列会失败...如果你想快速而又脏,你可以试一试抓住

try {
    db.execSQL("ALTER TABLE sometable ADD COLUMN newcolumn integer");   
}
catch(Exception e) {
    e.printStackTrace();
}

A better way is to move all of the data out of the table into a temp table, drop and re-create the table and put your data back in. Here is an example from something I've used before 更好的方法是将所有数据从表中移出到临时表中,删除并重新创建表并将数据重新放入。这是我之前使用的一个示例

//Update old Table
        if(DBVER< 1060){

            List<String> columns = DB.GetColumns(db, "listitems");
            db.execSQL("ALTER table listitems RENAME TO 'temp_listitems); ") ;
            String tblListItems = "create table if not exists listitems " +
                    "(id integer primary key autoincrement, " +
                    "listid integer, " +
                    "itemid integer, " +
                    "qty integer, " +
                    "collected integer, " +
                    "qtytype integer, " +
                    "tabid integer); " ;
            db.execSQL(tblListItems);
            columns.retainAll(DB.GetColumns(db, "listitems"));
            String cols = DB.join(columns, ","); 
            db.execSQL(String.format( "INSERT INTO %s (%s) SELECT %s from temp_%s", "listitems", cols, cols, "listitems"));
            db.execSQL("DROP table 'temp_" + "listitems");



        }

Heres a link http://www.devdaily.com/android/sqlite-alter-table-syntax-examples 下面是http://www.devdaily.com/android/sqlite-alter-table-syntax-examples的链接

 db.execSQL("ALTER TABLE " + MyTableName + " ADD "
           + MyNewColumn + " TEXT;"); 

Try it without Column: Syntax is: 不用Column就试试:语法是:

ALTER TABLE [TABLE_NAME] ADD [COLUMN_NAME] DATATYPE ALTER TABLE [TABLE_NAME] ADD [COLUMN_NAME] DATATYPE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM