Android 类似SQLite的选择不起作用
我正在尝试使用类似于%userCriteria%的LIKE语句查询我的数据库。我有一个包含以下列的表(存储在MySQLiteHelper中): 我想创建一个查询,只返回标题列中包含特定字符串的条目 我在DataSourceClass中实现的方法(应该执行查询)如下所示:Android 类似SQLite的选择不起作用,android,mysql,sqlite,Android,Mysql,Sqlite,我正在尝试使用类似于%userCriteria%的LIKE语句查询我的数据库。我有一个包含以下列的表(存储在MySQLiteHelper中): 我想创建一个查询,只返回标题列中包含特定字符串的条目 我在DataSourceClass中实现的方法(应该执行查询)如下所示: public ArrayList<RssItem> read(String selection, String[] selectionArgs, String groupBy, String having, Str
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;
}
这就是我在活动中调用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));
}
}
}
private void searchDatabase(字符串searchCriteriaTitle){
//清除适配器中的数据。。。
mAdapter.clear();
//获取所有存储的记录(在解析过程中)。。。
List items=rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE+“LIKE?”,新字符串[]{searchCriteriaTitle},null,null,null);
//如果搜索条件不是空字符串。。。
如果(!searchedItem.equals(“”){
对于(int i=0;i
现在返回的条目列表是空的,我不明白为什么。有什么建议吗 你试过了吗
List<RssItem> items = rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE + " LIKE ?", new String[]{"%"+searchCriteriaTitle+"%"},null,null,null);
List items=rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE+“LIKE?”,新字符串[]{“%”“%”+searchCriteriaTitle+“%”,null,null);
你试过了吗
List<RssItem> items = rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE + " LIKE ?", new String[]{"%"+searchCriteriaTitle+"%"},null,null,null);
List items=rssItemDataSource.read(MySQLiteHelper.COLUMN_TITLE+“LIKE?”,新字符串[]{“%”“%”+searchCriteriaTitle+“%”,null,null);
你能试试吗
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());
}
你能试试吗
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());
}
谢谢你的建议。我自己也尝试过使用rawQuery,但它看起来更复杂,所以我坚持使用query。感谢你的帮助:)@georgi koemdzhiev:很高兴你解决了它:)快乐的编码伙伴:)谢谢你的建议。我自己也尝试过使用rawQuery,但它看起来更复杂,所以我坚持使用query。感谢你的帮助:)@georgi koemdzhiev:很高兴你解决了它:)快乐的编码伙伴:)