SQLite rawquery 无法匹配预期的字符串
SQLite rawquery fails to match expected strings
我正在使用此方法进行简单的数据库搜索。
public Cursor queryExpense(String createdDate, String createdTime, String category, String description, String amount) {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_DATE + " LIKE ? AND " + COLUMN_TIME + " LIKE ? AND " +
COLUMN_CATEGORY + " LIKE ? AND " + COLUMN_DESCRIPTION + " LIKE ? AND " + COLUMN_AMOUNT + " LIKE ? ";
String mDate = createdDate.length() > 0 ? "%" + createdDate + "%" : "%";
String mTime = createdTime.length() > 0 ? "%" + createdTime + "%" : "%";
String mCategory = category.length() > 0 ? "%" + category + "%" : "%";
String mDescription = description.length() > 0 ? "%" + description + "%" : "%";
String mAmount = amount.length() > 0 ? "%" + amount + "%" : "%";
Log.d("Parameters",mDate + "," + mTime + "," + mCategory + "," + mDescription + "," + mAmount);
return db.rawQuery( query, new String[] { mDate, mTime, mCategory, mDescription, mAmount } );
}
}
不传值时(所有字段均为"%"),结果之一是这条记录:
Date: 30/10/2016
Time: 6:56:41
Category: Food
Description: Lunch at Nosh
Amount: 25
但是,将描述指定为 "unc" 时,不会出现相同的记录。调试日志显示了预期的参数和 SQL,所以我不确定为什么这不是结果。
SELECT * FROM expenses where createdDate like ? and createdTime like ? and category like ? and description like ? and amount like ?
[%,%,%,%unc%,%]
我错过了什么?
编辑:问题已解决
感谢@laalto,我已经确定问题是由于同时具有 Cursor.moveToFirst 和 Cursor.moveToNext,因此搜索可能会跳过一个结果。
基于问题评论:查询很好并且匹配预期的行。
问题是读取返回的 Cursor
。像
这样的结构
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
//...
}
}
跳过第一个结果行,这是唯一返回的具有更具体选择条件的行。
遍历游标的惯用模式是:
if (cursor.moveToFirst()) {
do {
//...
} while (cursor.moveToNext());
}
我正在使用此方法进行简单的数据库搜索。
public Cursor queryExpense(String createdDate, String createdTime, String category, String description, String amount) {
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_DATE + " LIKE ? AND " + COLUMN_TIME + " LIKE ? AND " +
COLUMN_CATEGORY + " LIKE ? AND " + COLUMN_DESCRIPTION + " LIKE ? AND " + COLUMN_AMOUNT + " LIKE ? ";
String mDate = createdDate.length() > 0 ? "%" + createdDate + "%" : "%";
String mTime = createdTime.length() > 0 ? "%" + createdTime + "%" : "%";
String mCategory = category.length() > 0 ? "%" + category + "%" : "%";
String mDescription = description.length() > 0 ? "%" + description + "%" : "%";
String mAmount = amount.length() > 0 ? "%" + amount + "%" : "%";
Log.d("Parameters",mDate + "," + mTime + "," + mCategory + "," + mDescription + "," + mAmount);
return db.rawQuery( query, new String[] { mDate, mTime, mCategory, mDescription, mAmount } );
}
}
不传值时(所有字段均为"%"),结果之一是这条记录:
Date: 30/10/2016
Time: 6:56:41
Category: Food
Description: Lunch at Nosh
Amount: 25
但是,将描述指定为 "unc" 时,不会出现相同的记录。调试日志显示了预期的参数和 SQL,所以我不确定为什么这不是结果。
SELECT * FROM expenses where createdDate like ? and createdTime like ? and category like ? and description like ? and amount like ?
[%,%,%,%unc%,%]
我错过了什么?
编辑:问题已解决
感谢@laalto,我已经确定问题是由于同时具有 Cursor.moveToFirst 和 Cursor.moveToNext,因此搜索可能会跳过一个结果。
基于问题评论:查询很好并且匹配预期的行。
问题是读取返回的 Cursor
。像
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
//...
}
}
跳过第一个结果行,这是唯一返回的具有更具体选择条件的行。
遍历游标的惯用模式是:
if (cursor.moveToFirst()) {
do {
//...
} while (cursor.moveToNext());
}