SQLite 游标查询错误 "like" 和“==”无法处理内容提供​​程序 "selection" 定义

SQLite cursor query error "like" and "==" not working on content provider "selection" definition

这可能是我的 SQLite "selector" 定义中的一个愚蠢错误,但我们似乎无法从迄今为止发布的类似问题和错误中找出它。我可能犯了一个愚蠢的错误,但我一辈子都找不到。如果您需要更多信息以便提供帮助,请告诉我!!

所以这非常有效:

selection = "((" +
                TransContract.TransEntry.COLUMN_DATE + " > " +
                Long.toString(dateOutput) + ") AND (" +
                TransContract.TransEntry.COLUMN_DATE + " <= " + Long.toString(dateOutputEnd) + "))";

但是当我尝试查询COLUMN_CATEGORY(TEXT格式)时,是这样的:

selection = TransContract.TransEntry.COLUMN_CATEGORY + " like " + '%'+
                "Transport General" + "%";

或者这样:

selection = TransContract.TransEntry.COLUMN_CATEGORY + " == " +
                "Transport General";

我得到一个"E/AndroidRuntime: FATAL EXCEPTION"

我的游标查询:

cursor = database.query(
                    TransContract.TransEntry.TABLE_NAME,
                    projection,
                    selection,
                    selectionArgs,
                    null,
                    null,
                    sortOrder);

我的 DbHelper(在其中找到 CATEGORY 列):

String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + TransContract.TransEntry.TABLE_NAME + " (" +
                    TransContract.TransEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    TransContract.TransEntry.COLUMN_AMOUNT + " REAL, " +
                    TransContract.TransEntry.COLUMN_TYPE + " INTEGER NOT NULL DEFAULT 0, " + // debit, credit
                    TransContract.TransEntry.COLUMN_ACCOUNT + " INTEGER, " + // wallet, savings, more later.
                    TransContract.TransEntry.COLUMN_ACCOUNT_FROM + " INTEGER, " + // defaults from wallet 0
                    TransContract.TransEntry.COLUMN_ACCOUNT_TO + " INTEGER, " + // defaults back to wallet 0
                    TransContract.TransEntry.COLUMN_CATEGORY + " TEXT, " +
                    TransContract.TransEntry.COLUMN_TITLE + " TEXT, " +
                    TransContract.TransEntry.COLUMN_DATE + " INTEGER, " +
                    TransContract.TransEntry.COLUMN_RECURRING + " INTEGER NOT NULL DEFAULT 0," +
                    TransContract.TransEntry.COLUMN_FREQUENCY + " TEXT," +
                    TransContract.TransEntry.COLUMN_END_DATE + " INTEGER" +
                    ");";

编辑

感谢您的意见,我应该更具体!我的堆栈跟踪确实是语法错误,它来自我粘贴的新代码行。

Caused by: android.database.sqlite.SQLiteException: near "General": syntax error (code 1): , while compiling: SELECT _id, category, title, amount, type, date FROM transactions WHERE category == Transport General ORDER BY date DESC 

问题是字符串的双引号内的单引号(感谢@Ton Plooij),但奇怪的是比较值不需要单引号。

这是可行的解决方案:

selection = TransContract.TransEntry.COLUMN_CATEGORY + " = " +
                "'Transport General'";  

我不知道你的编程语言是如何工作的,但你的两个失败的 SQlite 查询字符串似乎都被错误地创建了。类似查询应该类似于 "TransContract.TransEntry.COLUMN_CATEGORY like '%Transport General%'"。在查询中 构建比较字符串时没有左单引号和右单引号。第二个查询有两个问题:'==' 应该是单个 '=' 并且比较值没有用单引号引起来。