1

我尝试在我的数据库中切换 2 行的 ID。

我在这一行收到语法错误(代码 1):ourDatabase.update(LIVE_TABLE, cv, whereClause, whereArgs);

这很奇怪,因为如果我寻找的行不存在,if(c.getCount() < 1)那么应该给我回报。

这是错误代码:

06-15 11:27:19.225: W/dalvikvm(10025): threadid=1: thread exiting with uncaught exception (group=0x40cc6930)
06-15 11:27:19.235: E/AndroidRuntime(10025): FATAL EXCEPTION: main
06-15 11:27:19.235: E/AndroidRuntime(10025): android.database.sqlite.SQLiteException: near "79": syntax error (code 1): , while compiling: UPDATE live_table SET 79=? WHERE _id= ?
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1563)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at com.example.workoutlog.DataBaseMain.updateMoveLive(DataBaseMain.java:2050)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at com.example.workoutlog.LiveWorkoutPage.moveDown(LiveWorkoutPage.java:1756)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at com.example.workoutlog.LiveWorkoutPage.whatclicked(LiveWorkoutPage.java:1600)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at com.example.workoutlog.LiveWorkoutPage.onClick(LiveWorkoutPage.java:583)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.view.View.performClick(View.java:4211)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.view.View$PerformClick.run(View.java:17362)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.os.Handler.handleCallback(Handler.java:725)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.os.Looper.loop(Looper.java:137)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at android.app.ActivityThread.main(ActivityThread.java:5226)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at java.lang.reflect.Method.invokeNative(Native Method)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at java.lang.reflect.Method.invoke(Method.java:511)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-15 11:27:19.235: E/AndroidRuntime(10025):    at dalvik.system.NativeStart.main(Native Method)
06-15 11:27:37.122: I/Process(10025): Sending signal. PID: 10025 SIG: 9

这是我的代码:

public void updateMoveLive(String where, String to)
    {

        String whereClause = COLUMN_ID + "= ? ";  
        String[] whereArgs = new String[]{where};

        ContentValues cv = new ContentValues();

        String[] columns = new String[]{COLUMN_ID, COLUMN_ROUTINE_LIVE, COLUMN_INT};

        Cursor c = ourDatabase.query(LIVE_TABLE, columns, whereClause , whereArgs, null, null, null);

        if(c.getCount() < 1)
            return;

        int i = c.getColumnIndex(COLUMN_ID);
        c.moveToFirst();
        String tempID = c.getString(i);

        cv.put(to, COLUMN_ID);

        ourDatabase.update(LIVE_TABLE, cv, whereClause, whereArgs);

                whereClause = COLUMN_ID + "= ? ";
                whereArgs = new String[]{tempID};

                cv = new ContentValues();

                cv.put(where, COLUMN_ID);

                ourDatabase.update(LIVE_TABLE, cv, whereClause, whereArgs);
    }
4

1 回答 1

2

错误似乎在这一行:

cv.put(to, COLUMN_ID);

应该

cv.put(COLUMN_ID, to)

因为 ContentValue 默认是 ContentValue(KEY, VALUE)

编辑:事实上,下面还有另一个不正确的用法,关于

cv = new ContentValues();
cv.put(where, COLUMN_ID);

尝试修复这两个...

于 2013-06-15T09:14:38.867 回答