2014-06-09 39 views
0

升級只讀數據庫無法找出這一個。我正在升級數據庫,並且我創建了一個函數來重命名列,方法是將原始表重命名爲臨時表,然後使用重命名字段創建新表,然後將所有數據從臨時表複製到新表,但是失敗當到達複製數據的部分時。引起:android.database.sqlite.SQLiteException:無法從版本

功能執行重命名:

INSERT INTO vehicles (_id, vLabel, vYear, vMake, vModel, vOption, vDetail, vAdded) SELECT (id, vLabel, vYear, vMake, vModel, vOption, vDetail, vAdded) FROM tmp_vehicles 

然後我收到以下錯誤:

public void modifyColumn(SQLiteDatabase db, String oldColumnName, String newColumnName, String[] newColumnConstraints) { 

    // get current table columns 
    String sql = String.format("PRAGMA table_info('%s')", TABLE_NAME); 
    Cursor cursor = db.rawQuery(sql, new String[] {}); 

    int length = cursor.getCount(); 
    String[] oC = new String[length], nC = new String[length], c = new String[length]; 

    while(cursor.moveToNext()){ 
     int cid = cursor.getInt(cursor.getColumnIndex("cid")); 
     String name = cursor.getString(cursor.getColumnIndex("name")); 
     String type = cursor.getString(cursor.getColumnIndex("type")); 
     int notnull = cursor.getInt(cursor.getColumnIndex("notnull")); 
     String dflt = cursor.getString(cursor.getColumnIndex("dflt_value")); 
     int pk = cursor.getInt(cursor.getColumnIndex("pk")); 

     Vector<String> constraints = new Vector<String>(); 

     // add data type to field 
     constraints.add(type); 

     // add primary key option to field 
     if(pk > 0) 
      constraints.add("PRIMARY KEY"); 

     // add not null option to field 
     if(notnull > 0) 
      constraints.add("NOT NULL"); 

     // add default value to field 
     if(dflt != null) 
      constraints.add("DEFAULT " + dflt); 

     String cName = name; 
     String cType = TextUtils.join(" ", constraints.toArray()); 

     oC[cid] = cName; 
     cName = (cName.equals(oldColumnName))? newColumnName : cName; 
     nC[cid] = cName; 

     if(newColumnConstraints != null) 
      cType = TextUtils.join(" ", newColumnConstraints); 

     c[cid] = cName + " " + cType; 
    } 


    // rename old table 
    this.renameTable(db, "tmp_" + TABLE_NAME); 

    // create new table with modified column 
    this.createTable(db, TABLE_NAME, c); 

    // copy data from temporary table to new table 
    sql = "INSERT INTO " + TABLE_NAME + " (" + TextUtils.join(", ", nC) + 
      ") SELECT (" + TextUtils.join(", ", oC) + ") FROM tmp_" + TABLE_NAME; 
    db.execSQL(sql); 

    // drop temporary table 
    db.execSQL("DROP TABLE tmp_" + TABLE_NAME); 
} 

在註釋的「從臨時表到新表中複製數據」,是執行看起來像之前的SQL輸出:

06-08 19:33:05.888: E/Database(21562): Failure 1 (near ",": syntax error) on 0x2037d8 when preparing 'INSERT INTO vehicles (_id, vLabel, vYear, vMake, vModel, vOption, vDetail, vAdded) SELECT (id, vLabel, vYear, vMake, vModel, vOption, vDetail, vAdded) FROM tmp_vehicles'. 
06-08 19:33:08.711: W/dalvikvm(21562): threadid=1: thread exiting with uncaught exception (group=0x4001f560) 
06-08 19:33:08.771: E/AndroidRuntime(21562): FATAL EXCEPTION: main 
06-08 19:33:08.771: E/AndroidRuntime(21562): java.lang.RuntimeException: Unable to start activity 
06-08 19:33:08.771: E/AndroidRuntime(21562): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1648) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1662) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at android.os.Looper.loop(Looper.java:130) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at android.app.ActivityThread.main(ActivityThread.java:3696) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at java.lang.reflect.Method.invokeNative(Native Method) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at java.lang.reflect.Method.invoke(Method.java:507) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 
06-08 19:33:08.771: E/AndroidRuntime(21562): at dalvik.system.NativeStart.main(Native Method) 
+0

爲什麼不直接運行alter table並重命名現有表中的字段? – k3v1n4ud3

回答

1

select語法錯誤。

替換

SELECT (column1, column2, ...) 

SELECT column1, column2, ... 

即刪除括號。

相關問題