如何比较以文本形式存储的sqlite中的日期和时间?
How to compare date and time in sqlite which is stored as text?
我在 sqlite 数据库中有一列以这种格式 (YYYY-MM-DD HH:MM) 存储日期和时间。当我尝试将此列与另一个日期和时间值进行比较时,我的应用程序崩溃了。我猜这是因为 (HH:MM) 中存在冒号。我该如何比较?
查询:
public Cursor fetchrDatetime(String dati) throws SQLException {
String query = "SELECT * FROM reminders WHERE reminder_date_time=" + dati;
Cursor cursor = mDb.rawQuery(query, null);
return cursor;
}
日志猫:
android.database.sqlite.SQLiteException: near "16": syntax error (code 1): , while compiling: SELECT * FROM reminders WHERE reminder_date_time=2015-01-09 16:14
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.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 apps.rwx.doctorremainder.RemindersDbAdapter.fetchrDatetime(RemindersDbAdapter.java:248)
at apps.rwx.doctorremainder.ReminderEditActivity.saveState(ReminderEditActivity.java:306)
at apps.rwx.doctorremainder.ReminderEditActivity.access0(ReminderEditActivity.java:40)
at apps.rwx.doctorremainder.ReminderEditActivity.onClick(ReminderEditActivity.java:203)
at android.view.View.performClick(View.java:4091)
at android.view.View$PerformClick.run(View.java:17072)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:4987)
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:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
WHERE
的值应介于 '
.
之间
String query = "SELECT * FROM reminders WHERE reminder_date_time='" + dati + "'";
我在 sqlite 数据库中有一列以这种格式 (YYYY-MM-DD HH:MM) 存储日期和时间。当我尝试将此列与另一个日期和时间值进行比较时,我的应用程序崩溃了。我猜这是因为 (HH:MM) 中存在冒号。我该如何比较?
查询:
public Cursor fetchrDatetime(String dati) throws SQLException {
String query = "SELECT * FROM reminders WHERE reminder_date_time=" + dati;
Cursor cursor = mDb.rawQuery(query, null);
return cursor;
}
日志猫:
android.database.sqlite.SQLiteException: near "16": syntax error (code 1): , while compiling: SELECT * FROM reminders WHERE reminder_date_time=2015-01-09 16:14
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.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 apps.rwx.doctorremainder.RemindersDbAdapter.fetchrDatetime(RemindersDbAdapter.java:248)
at apps.rwx.doctorremainder.ReminderEditActivity.saveState(ReminderEditActivity.java:306)
at apps.rwx.doctorremainder.ReminderEditActivity.access0(ReminderEditActivity.java:40)
at apps.rwx.doctorremainder.ReminderEditActivity.onClick(ReminderEditActivity.java:203)
at android.view.View.performClick(View.java:4091)
at android.view.View$PerformClick.run(View.java:17072)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:4987)
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:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
WHERE
的值应介于 '
.
String query = "SELECT * FROM reminders WHERE reminder_date_time='" + dati + "'";