更新时间戳时出现Sqliteexception?

时间:2012-02-13 07:52:51

标签: android sqlite timestamp

我有这段代码:

      String sql="UPDATE  LAST_OPEN  SET  LAST_OPEN="+DATE+"  WHERE  STUDENT_ID ="+STUDENT_ID+"    AND   ITEM_ID="+ITEM_ID+" AND   ITEM_NAME="+ITEM_TYPE+";";

db.execSQL(sql);

其中LAST_OPEN字段是时间戳。我试图在SQlite浏览器中运行相同的查询它工作正常。但是当在应用程序中执行此Sql时它显示:

       02-13 13:12:39.468: E/AndroidRuntime(2366): FATAL EXCEPTION: main
       02-13 13:12:39.468: E/AndroidRuntime(2366): android.database.sqlite.SQLiteException: near "13": syntax error: UPDATE  LAST_OPEN  SET  LAST_OPEN=2012-02-13 13:12:39  WHERE  STUDENT_ID =5    AND   ITEM_ID=1 AND   ITEM_NAME=Activity;
       02-13 13:12:39.468: E/AndroidRuntime(2366):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
       -13 13:12:39.468: E/AndroidRuntime(2366):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
        02-13 13:12:39.468: E/AndroidRuntime(2366):     at com.cuelearn.databases.last_open.update(last_open.java:205)
      02-13 13:12:39.468: E/AndroidRuntime(2366):   at com.cuelearn.main.threeshelf$1.onClick(threeshelf.java:197)
      02-13 13:12:39.468: E/AndroidRuntime(2366):   at android.view.View.performClick(View.java:2408)
       02-13 13:12:39.468: E/AndroidRuntime(2366):  at android.view.View$PerformClick.run(View.java:8816)
     02-13 13:12:39.468: E/AndroidRuntime(2366):    at android.os.Handler.handleCallback(Handler.java:587)
      02-13 13:12:39.468: E/AndroidRuntime(2366):   at android.os.Handler.dispatchMessage(Handler.java:92)
       02-13 13:12:39.468: E/AndroidRuntime(2366):  at android.os.Looper.loop(Looper.java:123)
   02-13 13:12:39.468: E/AndroidRuntime(2366):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    02-13 13:12:39.468: E/AndroidRuntime(2366):     at java.lang.reflect.Method.invokeNative(Native Method)
     02-13 13:12:39.468: E/AndroidRuntime(2366):    at java.lang.reflect.Method.invoke(Method.java:521)
       02-13 13:12:39.468: E/AndroidRuntime(2366):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        02-13 13:12:39.468: E/AndroidRuntime(2366):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

谁能告诉我哪里出错?

2 个答案:

答案 0 :(得分:1)

尝试使用此sql语句:

String sql="UPDATE LAST_OPEN SET LAST_OPEN='"+DATE+"' WHERE STUDENT_ID ="+STUDENT_ID+" AND ITEM_ID="+ITEM_ID+" AND ITEM_NAME="+ITEM_TYPE+";";

据我所知,当你在sql语句中使用任何字符串数据时,你需要在变量的开头和结尾添加''

答案 1 :(得分:1)

尝试更改

LAST_OPEN=2012-02-13 13:12:39

LAST_OPEN = "2012-02-13 13:12:39"

数字之间的空格可能是个问题。希望这会有所帮助。