我正在尝试使用像%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;
}
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
};
这就是我在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));
}
}
}
现在返回的条目列表是空的,我不明白为什么。有什么建议?
答案 0 :(得分:2)
你试过吗?
List<RssItem> items = rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE + " LIKE ?", new String[]{"%"+searchCriteriaTitle+"%"},null,null,null);
答案 1 :(得分:0)
你能试试吗
Cursor cursor = mDatabase rawQuery("select "+COLUMN_TITLE +"from "+MySQLiteHelper.TABLE_NAME+" where "+MySQLiteHelper.COLUMN_TITLE+" LIKE \'?\'", new String[]{"userCriteria"});
if(cursor.moveToFirst()){
do {
RssItem task = cursorToTask(cursor);
tasks.add(task);
cursor.moveToNext();
}while (cursor.moveToNext());
}