尝试执行一个简单的查询,获取语法错误:在where子句中的代码1错误。

时间:2014-02-03 05:29:41

标签: android sqlite

我正在尝试更新一行数据库的内容,这是我的查询:

public void updateAdhoc(String value1, String value2, String value3,String CONTACT_ID){

        int id = Integer.valueOf(CONTACT_ID);
        String update = "UPDATE Adhoc SET Name = " + "'"+value1+"'" +","+
                "DOB =  " + "'"+value2+"'" +","+
                "ImageData =  " + "'"+value3+"'" +","+
                "where " +adhocIdPrimary +"= " +id+";"; 

        mDb.execSQL(update);
    }

代码在where子句中失败。以下是我的表创建字符串:

private static final String DATABASE_CREATE3 = "create table Adhoc (_id integer primary key autoincrement, "
            +"DOB text, " +
            "Name text not null, " +
            "ImageData text);";
    static final String DATABASE_TABLE3 = "Adhoc";

以下是引用列名的变量:

//Table Adhoc 
public static final String adhocIdPrimary = "_id";
public static final String adhocName = "Name";
public static final String adhocDob = "DOB";
public static final String adhocImageData = "ImageData";

LogCat:

02-03 10:57:50.017: E/AndroidRuntime(9576): FATAL EXCEPTION: main
02-03 10:57:50.017: E/AndroidRuntime(9576): android.database.sqlite.SQLiteException: near "where": syntax error (code 1): , while compiling: UPDATE Adhoc SET Name = 'Ram Lal',DOB =  '2014-2-3 ',ImageData =  '/data/data/com.ex.xxx/files/xxx/adhoc1391405269898.jpg',where _id= 1;
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at com.exa.birthdayrem.DBAdapter.updateAdhoc(DBAdapter.java:421)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at com.ex.xxx.AdhocEdit$3.onClick(AdhocEdit.java:207)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.view.View.performClick(View.java:4091)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.view.View$PerformClick.run(View.java:17072)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.os.Handler.handleCallback(Handler.java:615)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.os.Looper.loop(Looper.java:153)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at android.app.ActivityThread.main(ActivityThread.java:5000)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at java.lang.reflect.Method.invoke(Method.java:511)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-03 10:57:50.017: E/AndroidRuntime(9576):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:5)

,字前有一个不需要的where逗号。只需删除它然后重新运行。

String update = "UPDATE Adhoc SET Name = " + "'"+value1+"'" +","+
                "DOB =  " + "'"+value2+"'" +","+
                "ImageData =  " + "'"+value3+"'" +","+   // remove this last comma
                "where " +adhocIdPrimary +"= " +id+";"; 

答案 1 :(得分:2)

删除,之前的WHERE,并在adhocIdPrimary

之后提供空格

看起来像

String update = "UPDATE Adhoc SET Name = " + "'"+value1+"'" +","+
            "DOB =  " + "'"+value2+"'" +","+
            "ImageData =  " + "'"+value3+"'" +" "+
            "where " +adhocIdPrimary +" = " +id+";"; `

答案 2 :(得分:2)

您需要删除where之前的逗号并将其替换为空格:

String update = "UPDATE Adhoc SET Name = " + "'"+value1+"'" +","+
            "DOB =  " + "'"+value2+"'" +","+
            "ImageData =  " + "'"+value3+"'" +" "+
            "where " +adhocIdPrimary +"= " +id+";";