如何修复Android SQLITE_ERROR,错误代码1

时间:2019-02-13 04:31:34

标签: android sqlite

试图获取一个包含日期和相​​关注释的表。我正在发送一个请求,以获取日期=我决定的表和数据。

我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ar.calendar13, PID: 22214
android.database.sqlite.SQLiteException: near "Feb": syntax error (code 1): , while compiling: SELECT CAL_EVENT_ID, CAL_EVENT_TTTLE, CAL_EVENT_NOTES, CAL_REMINDER_DATE, CAL_EVENT_REPEAT FROM CAL_EVENT_TABLE WHERE CAL_REMINDER_DATE = Tue Feb 12 00:00:00 EST 2019
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
    (near "Feb": syntax error (code 1): , while compiling: SELECT CAL_EVENT_ID, CAL_EVENT_TTTLE, CAL_EVENT_NOTES, CAL_REMINDER_DATE, CAL_EVENT_REPEAT FROM CAL_EVENT_TABLE WHERE CAL_REMINDER_DATE = Tue Feb 12 00:00:00 EST 2019)
#################################################################
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1096)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:661)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
    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:1746)
    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1593)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1464)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1632)
    at com.example.ar.calendar13.Cal_DBManager.fetchDayRecords(Cal_DBManager.java:85)

DB-MANAGER:

 public Cursor fetchDayRecords(String strCalDate) {
    String[] columns = new String[]{
            Cal_DBHelper.CAL_EVENT_ID, Cal_DBHelper.CAL_EVENT_TTTLE,
            Cal_DBHelper.CAL_EVENT_NOTES, Cal_DBHelper.CAL_REMINDER_DATE,
            Cal_DBHelper.CAL_EVENT_REPEAT
    };

    Cursor cursor = database.query(Cal_DBHelper.TABLE_NAME1, columns,
            Cal_DBHelper.CAL_REMINDER_DATE + " = " + strCalDate,
            null, null, null, null);

    if (cursor != null) {
        cursor.moveToFirst();
    }//if

    return cursor;
}//displayRecordTable1

DBHELPER:

 private static final String CREATE_TABLE1 = "create table " + TABLE_NAME1 + "(" + CAL_EVENT_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CAL_EVENT_TTTLE + " TEXT NOT NULL, " +
        CAL_EVENT_NOTES + " TEXT NOT NULL, " + CAL_REMINDER_DATE + " TEXT NOT NULL, " +
        CAL_EVENT_REPEAT + " INTEGER);";

可以调用“ FETCHDAYRECORDS”的我的代码:

final Cursor cursor = dbManager.fetchDayRecords(dateClicked.toString());
    dbNumItems = cursor.getCount();

dateCLicked是一个看起来像这样的字符串值(例如): 美国东部时间2月12日星期二00:00:00

为什么会出现错误???它说我的错误在“ Feb”附近

是因为表为空吗?我对此表示怀疑,我正在检查是否有任何记录...

任何帮助都会有所帮助!

提前谢谢!

托尼

2 个答案:

答案 0 :(得分:1)

只需加上引号

Cursor cursor = database.query(Cal_DBHelper.TABLE_NAME1, columns,
            Cal_DBHelper.CAL_REMINDER_DATE + " = "+" " " + strCalDate +" " ",
            null, null, null, null);

SELECT
  CAL_EVENT_ID,
  CAL_EVENT_TTTLE,
  CAL_EVENT_NOTES,
  CAL_REMINDER_DATE,
  CAL_EVENT_REPEAT
FROM
  CAL_EVENT_TABLE
WHERE
  CAL_REMINDER_DATE = "Tue  Feb 12 00 :00 :00 EST 2019"

您的查询应该是这样

答案 1 :(得分:1)

您的问题是WHERE子句中的日期(即EST 2019年2月12日星期二00:00:00)没有用引号引起来。

如果使用查询方法的第4个参数,即指定选择参数(string [])并在WHERE子句(第3个参数)中包含占位符(?),则除了文字正确地用引号引起来,您还可以防止SQL注入。

例如您可以使用

public Cursor fetchDayRecords(String strCalDate) {
    String[] columns = new String[]{
            Cal_DBHelper.CAL_EVENT_ID, Cal_DBHelper.CAL_EVENT_TTTLE,
            Cal_DBHelper.CAL_EVENT_NOTES, Cal_DBHelper.CAL_REMINDER_DATE,
            Cal_DBHelper.CAL_EVENT_REPEAT
    };

    Cursor cursor = database.query(Cal_DBHelper.TABLE_NAME1, columns,
            Cal_DBHelper.CAL_REMINDER_DATE + " =?", new String[]{strCalDate},
            null, null, null);
    cursor.moveToFirst();
    return cursor;

}//displayRecordTable1
  • 请注意,从查询方法返回的游标永远不会为空,因此if (cursor !- null)无效,因此已被删除。