Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

android - How to solve Syntax error in Update query in Sqlite Database

I am updating Status_msg column using Update query in Sqlite Database by value 1 to 0. The Syntax error occurred when compiling the application.

The 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)

The Update function is given below

    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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
String status_change_query = "UPDATE " + DB_Table1 + " SET " +  Status_Msg + " = 0 where " + Status_Msg + " = 1";

gives

UPDATE sms_tbl SET 1 = 0 where 1 = 1 at 

In other words, you seem to have simply mixed up your variables.

Status_Msg should be your column name but it seems to instead be a numeric value, and the resulting query is invalid SQL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...