我正在尝试使用类似 %userCriteria% 的 LIKE 语句查询我的数据库。我有一个包含以下列的表(存储在 MySQLiteHelper 中):
MySQLiteHelper.COLUMN_DESCRIPTION,
MySQLiteHelper.COLUMN_PUB_DATE,
MySQLiteHelper.COLUMN_TITLE,
MySQLiteHelper.COLUMN_USER_SPEC_NAME,
MySQLiteHelper.COLUMN_LINK
我想创建一个查询,该查询将仅返回 TITLE 列中包含特定字符串的条目。
我正在实现的方法(应该执行查询)在我的 dataSourseClass 中,如下所示:
public ArrayList<RssItem> read(String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
ArrayList<RssItem> tasks = new ArrayList<RssItem>();
Cursor cursor = mDatabase.query(MySQLiteHelper.TABLE_NAME, new String[]{allColumns[3]}, selection, selectionArgs, groupBy, having, orderBy);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
RssItem task = cursorToTask(cursor);
tasks.add(task);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return tasks;
}
所有列:
private String[] allColumns = {
MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_DESCRIPTION,
MySQLiteHelper.COLUMN_PUB_DATE,
MySQLiteHelper.COLUMN_TITLE,
MySQLiteHelper.COLUMN_USER_SPEC_NAME,
MySQLiteHelper.COLUMN_LINK
};
这就是我在 Activity 中调用 read 方法的方式:
private void searchDatabase(String searchCriteriaTitle) {
//clear the data from the adapter...
mAdapter.clear();
//get all of the stored records (during the parsing process)...
List<RssItem> items = rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE + " LIKE ?",new String[]{searchCriteriaTitle},null,null,null);
//if the search criteria is not empty string...
if(!searchedItem.equals("")){
for(int i = 0; i < items.size();i++) {
//search from a match against the user's search criteria...
mAdapter.add(items.get(i));
}
}
//if the search criteria is am empty string...
else{
//read all of the
List<RssItem> allItems = rssItemDataSource.getAllTasks();
//add all...
for(int i = 0; i < allItems.size();i++) {
mAdapter.add(allItems.get(i));
}
}
}
现在返回的条目列表为空,我不明白为什么。有什么建议么?
最佳答案
你尝试过吗?
List<RssItem> items = rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE + " LIKE ?", new String[]{"%"+searchCriteriaTitle+"%"},null,null,null);
关于android - SQLite LIKE 选择不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36425986/