android.database.sqlite.SQLiteException: near “…”: syntax error (code 1)

后端 未结 4 687
猫巷女王i
猫巷女王i 2021-01-14 15:19

When i open my RSS app, i would like to check if an article exists in my database.If it exists,i would like to delete it (or ignore it),if not to write it in the db.

4条回答
  •  我在风中等你
    2021-01-14 15:52

    This is a typical issue with not using the selectionArgs, and it is exactly why one should use them: You are quoting your string with simple quotes, but your string contains single quote, so SQLite detect the end of the string before it actually ends, and try to parse the rest as SQLite keyword.

    The proper solution is to leave the escaping to SQLite, by using those selectionArgs rather that trying to do the escaping your self. in you case, that would be :

    Cursor cursor = ourDatabase.rawQuery("select 1 from "
                + DBHelper.DATABASE_TABLE + " where " + DBHelper.TITLE + "=?"
                + " AND " + DBHelper.DATE + "=?",
                new String[] {title, date});
    

    plus, it's cleaner because you can make a constant out of your query rather than constructing it every time.

提交回复
热议问题