这可能是我的SQLite“选择器”定义上的一个愚蠢的错误,但似乎无法从我们迄今发布的类似问题和错误中看出来。我可能犯了一个愚蠢的错误,但不能为我的生活找到它。如果您需要更多信息以便能够提供帮助,请告知我们。
所以这非常有效:
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'";
答案 0 :(得分:1)
我不知道您的编程语言是如何工作的,但是您的SQlite查询字符串失败似乎都是错误的。类似查询应该看起来像“TransContract.TransEntry.COLUMN_CATEGORY”,如'%Transport General%'“。构造比较字符串时,查询中没有打开和关闭单引号 。第二个查询有两个问题:'=='应该是单个'=',并且比较值不会用单引号括起来。