无法从SQLite

时间:2017-05-14 06:32:22

标签: android sqlite date intervals

我正在创建提醒应用程序,并在其中我在SQLite中添加提醒详细信息。我将日期添加为dd/MM/yyyy HH:mm:ss格式的字符串。但是当我试图获得接下来的7天数据时,我会得到以下异常。

FATAL EXCEPTION: main
 Process: com.sandy.remindcall, PID: 31982
android.database.sqlite.SQLiteException: near "11": syntax error (code 1): , while compiling: SELECT * FROM call_reminder WHERE reminder_date_time BETWEEN 21/05/2017 11:49:19 AND 14/05/2017 11:49:19
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)   

这是我的代码。

public Cursor getNextSevenDaysRecord(){
    Calendar theEnd = Calendar.getInstance();
    Calendar theStart = (Calendar) theEnd.clone();

    theStart.add(Calendar.DAY_OF_MONTH, +7);

    SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DD_MM_YYYY_HH_MM_SS);
    String start = dateFormat.format(theStart.getTime());
    String end = dateFormat.format(theEnd.getTime());

    // Now you have date boundaries in TEXT format
    Cursor cursor = mDb.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE "+KEY_DATE_TIME+" BETWEEN "+start+" AND "+end,null);

    return cursor;
}   

请给我任何参考

2 个答案:

答案 0 :(得分:2)

错误是由于未在SQL语句中引用字符串值引起的。但即使在引用它之后也无法正常工作,因为这些值将被比作字符串而不是日期,所以14/05/2017将在2015年5月19日之前以词汇顺序进行比较。

要解决此问题,请将日期存储为数字,或使用前面年份,月份,日期等格式。

答案 1 :(得分:0)

您可以存储自1970年以来的秒/毫秒时间,这将使比较变得非常简单,因为它正在比较两个Integer&#39}。 calendar.getTimeInMillis()以毫秒为单位返回它。您甚至可以使用SQLite的时间函数:https://www.sqlite.org/lang_datefunc.html

相关问题