我有一个包含日期字段的表。我在将表创建为Integer时保存了日期字段。用户使用日期选择器选择日期,日期以以下格式存储:dd / mm / yy。 我想返回表格中日期在字段日期>中的行。当前的日期 我尝试了以下查询:
public Cursor getUpcomAp ( ) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery (" select * from " + TABLE_NAME2 + "where strftime('%d.%m.%Y', date) > now.strftime('%d.%m.%Y')" , null);
return res;
}
单击执行此查询的按钮时,应用程序崩溃。 这是logcat输出:
01-02 13:09:39.250: D/(918): HostConnection::get() New Host Connection established 0xb882df30, tid 918
01-02 13:09:39.400: W/EGL_emulation(918): eglSurfaceAttrib not implemented
01-02 13:09:39.410: D/OpenGLRenderer(918): Enabling debug mode 0
01-02 13:09:57.690: D/dalvikvm(918): GC_FOR_ALLOC freed 109K, 6% free 3263K/3448K, paused 197ms, total 210ms
01-02 13:09:58.570: W/EGL_emulation(918): eglSurfaceAttrib not implemented
01-02 13:10:01.770: E/SQLiteLog(918): (1) near "(": syntax error
01-02 13:10:01.780: D/AndroidRuntime(918): Shutting down VM
01-02 13:10:01.780: W/dalvikvm(918): threadid=1: thread exiting with uncaught exception (group=0xb1a5eba8)
01-02 13:10:01.820: E/AndroidRuntime(918): FATAL EXCEPTION: main
01-02 13:10:01.820: E/AndroidRuntime(918): Process: com.example.appointapp, PID: 918
01-02 13:10:01.820: E/AndroidRuntime(918): android.database.sqlite.SQLiteException: near "(": syntax error (code 1): , while compiling: select * from appointments_table where strftime ('%d.%m.%Y', date) > now.strftime('%d.%m.%Y')
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-02 13:10:01.820: E/AndroidRuntime(918): at com.example.appointapp.DatabaseHelper.getUpcomAp(DatabaseHelper.java:86)
01-02 13:10:01.820: E/AndroidRuntime(918): at com.example.appointapp.AppointFragment$5.onClick(AppointFragment.java:189)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.view.View.performClick(View.java:4438)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.view.View$PerformClick.run(View.java:18422)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.os.Handler.handleCallback(Handler.java:733)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.os.Looper.loop(Looper.java:136)
01-02 13:10:01.820: E/AndroidRuntime(918): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-02 13:10:01.820: E/AndroidRuntime(918): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 13:10:01.820: E/AndroidRuntime(918): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 13:10:01.820: E/AndroidRuntime(918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-02 13:10:01.820: E/AndroidRuntime(918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-02 13:10:01.820: E/AndroidRuntime(918): at dalvik.system.NativeStart.main(Native Method)
请帮忙
答案 0 :(得分:4)
WHERE
之前需要一个空格:
"where strftime('%d.%m.%Y', date)....
应该是
" where strftime('%d.%m.%Y', date)...
(SELECT * FROM table_nameWHERE...
,这是您的结果)
答案 1 :(得分:1)
@IBAction func coinChanged(sender: UIButton) {
if sender.titleLabel?.text == "Went 1st" {
coin = true
sender.setTitle("Went 2nd", forState: UIControlState.Normal)
} else {
coin = false
sender.setTitle("Went 1st", forState: UIControlState.Normal)
}
}
where strftime('%d.%m.%Y', date) > now.strftime('%d.%m.%Y')
始终希望将数据值作为参数。 (并且strftime()
不是特殊的列名;只有字符串now
才会被专门处理。)
此外,'now'
不是supported date formats之一; dd/mm/yy
只为您的值返回NULL。此外,格式为strftime()
的字符串无法与字符串比较正确比较。
以dd.mm.yyyy
等受支持的格式存储日期。然后你可以直接比较这些值:
yyyy-mm-dd
答案 2 :(得分:0)
必须在TABLE_NAME2
和where
:
Cursor res = db.rawQuery (" select * from " + TABLE_NAME2 + " where strftime('%d.%m.%Y', date) > now.strftime('%d.%m.%Y')" , null);