android - SQLite LIKE 选择不起作用

标签 android mysql sqlite

我正在尝试使用类似 %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/

相关文章:

Androidplot:创建时间轴

php - 如何模拟 "MYSQL has gone away"错误?

python - 如何更新最新的sqlite3帖子?

android - Admob 插页式广告不会显示

android - 如何在 Android 中制作 Top Dialog 或 sheet?

java - 如何设置这个 View ?

mysql - 在 codeigniter 事件记录方法中使用 MySQL 函数

PHP 获取一个脚本,该脚本将向数据库中的所有电子邮件发送新闻通讯

sql-server - 在Django数据库中插入多个值-原始sqlite语句

dll - sqlite3 扩展函数 : The specified module could not be found