【问题标题】:How to search a String from multiple columns in an Sqlite database and returns the results in listview?如何从 Sqlite 数据库中的多个列中搜索字符串并在列表视图中返回结果?
【发布时间】:2018-10-29 23:22:53
【问题描述】:

我正在尝试从我的数据库中搜索一个主要包含 3 列(问题、答案和 cmets)的字符串,因此用户可以搜索一个单词,并且该单词在任何列中的任何实例都应该出现在搜索中结果。 我使用了这段代码,但它只返回第一列的搜索。你知道我的代码有什么问题吗:

public Cursor search(String searchString) {

    String[] columns = new String[]{ KEY_QUESTION,KEY_ANSWER, KEY_COMMENT};
    String where =  KEY_QUESTION + " LIKE ? OR "+ KEY_ANSWER  + " LIKE ? ";
    searchString = "%" + searchString + "%";
    String[] whereArgs = new String[]{searchString};

    Cursor cursor = null;
    try {if (database == null) {
        database = openHelper.getReadableDatabase();
    }

        cursor = database.query(TABLE_FLASH, columns, where, whereArgs, null, null, null);
    } catch (Exception e) {
        Log.d(TAG, "SEARCH EXCEPTION! " + e); // Just log the exception
    }
    return cursor;
}

【问题讨论】:

    标签: java android sqlite cursor android-sqlite


    【解决方案1】:

    您只提供一个参数,但您的查询有两个,所以第二个将为空。 试试这个:

    String[] whereArgs = new String[]{searchString, searchString};
    

    【讨论】:

    • 非常感谢 Okas,你让我开心。我的理解是,对于每一列,我应该在 whereArgs 数组中添加一个新的“searchString”。好吧,您是否建议使用特定文档来深入我的理解?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2017-11-09
    • 2013-12-24
    • 2020-12-28
    相关资源
    最近更新 更多