如何解决Sqlite数据库中更新查询中的语法错误

时间:2016-02-20 09:53:33

标签: android sqlite

  

我在Sqlite数据库中使用Status_msg查询按值1更新Update列。编译应用程序时出现语法错误。

     

Log cat

 02-20 09:45:36.145 19350-19350/com.smsfilter E/SQLiteLog: (1) near "1": syntax error
    02-20 09:45:36.145 19350-19350/com.smsfilter D/AndroidRuntime: Shutting down VM
    02-20 09:45:36.145 19350-19350/com.smsfilter W/dalvikvm: threadid=1: thread   exiting with uncaught exception (group=0x40a71930)
   02-20 09:45:36.195 19350-19350/com.smsfilter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                android.database.sqlite.SQLiteException: near "1": syntax error (code 1): ,  while compiling: UPDATE sms_tbl SET 1 = 0 where 1 = 1
                                                                   at  android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                   at         android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnecti on.java:882)
                                                                   at  android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
                                                                   at  android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                   at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                   at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                   at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
                                                                   at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
                                                                   at com.smsfilter.DB_Message.setStatus(DB_Message.java:106)
                                                                   at com.smsfilter.SpamActivity$datalist$1.onCheckedChanged(SpamActivity.java:208)
                                                                   at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
                                                                   at android.widget.CompoundButton.toggle(CompoundButton.java:87)
                                                                   at android.widget.CompoundButton.performClick(CompoundButton.java:99)
                                                                   at android.view.View$PerformClick.run(View.java:17355)
                                                                   at android.os.Handler.handleCallback(Handler.java:725)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                   at android.os.Looper.loop(Looper.java:137)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:511)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                   at dalvik.system.NativeStart.main(Native Method)
  

更新功能如下所示

    public int setStatus(){
    SQLiteDatabase sqldb = this.getWritableDatabase();
    String status_change_query = "UPDATE " + DB_Table1 + " SET " +  Status_Msg + " = 0 where " + Status_Msg + " = 1";
    Cursor cursor = sqldb.rawQuery(status_change_query,null);
    cursor.close();
    System.out.println(""+ Status_Msg);
    sqldb.close();
    return Status_Msg;
}

1 个答案:

答案 0 :(得分:1)

String status_change_query = "UPDATE " + DB_Table1 + " SET " +  Status_Msg + " = 0 where " + Status_Msg + " = 1";

给出

UPDATE sms_tbl SET 1 = 0 where 1 = 1 at 

换句话说,您似乎只是混淆了您的变量。

Status_Msg应该是您的列名,但它似乎是一个数值,并且生成的查询是无效的SQL。