我想用像%userCriteria%這樣的LIKE語句來查詢我的數據庫。我有一個表有以下的列(存儲在MySQLiteHelper):SQLite LIKE選擇不起作用
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;
}
allColumns:
private String[] allColumns = {
MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_DESCRIPTION,
MySQLiteHelper.COLUMN_PUB_DATE,
MySQLiteHelper.COLUMN_TITLE,
MySQLiteHelper.COLUMN_USER_SPEC_NAME,
MySQLiteHelper.COLUMN_LINK
};
這是我如何打電話我的活動中的閱讀方法:
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));
}
}
}
現在,返回的條目列表是空的,我不明白爲什麼。有什麼建議麼?
謝謝。這就是訣竅!欣賞它:) –
@GeorgiKoemdzhiev喲歡迎budy! –