gpt4 book ai didi

java - 在 Android Content Provider 中执行 'like' 查询

转载 作者:行者123 更新时间:2023-12-01 16:55:37 26 4
gpt4 key购买 nike

我正在 Android 上实现一个内容提供程序。我希望我的查询不仅仅查找精确匹配 - 有没有办法在内容提供程序查询方法中指定“类似”查询?

当前代码。请注意,mSearchString 是用户提供的字符串 - 我当前正在使用它来查询 UserDictionary,并且它返回任何内容的唯一方法是 if mSearchStringUserDictionary 中的单词完全匹配。我希望能够匹配部分查询(例如,如果字典中包含 apple、aunt 和practical,我想搜索“a”并返回全部)。

        mSelectionClause = UserDictionary.Words.WORD + " = ?";

// Moves the user's input string to the selection arguments.
// Remember to add code here to check for invalid or malicious input.
mSelectionArgs[0] = mSearchString;

// Does a query against the table and returns a Cursor object
Cursor mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause, // Either null, or the word the user entered
mSelectionArgs, // Either empty, or the string the user entered
mSortOrder);

最佳答案

我正在使用SQLiteDatabase (不使用getContentResolver(),我不知道它是什么),但我可以搜索单词like使用此代码的给定模式(名为 pattern ),因此由于语法看起来相同,我想它可以为您工作:

    String whereClause = "word like ?";
String[] wildCardReplacements = {pattern};
cursor = database.query(TABLE_NAME, mColumns, whereClause,
wildCardReplacements, null, null, null);

所以如果 pattern包含%a% ,我会得到“苹果,阿姨,还有,实用”和很多其他的。

如果这不明显,在你的情况下,类似mSearchString = "%" + mSearchString + "%"将允许用户仅输入 a (例如)并获得所需的匹配。

附注database定义为 android.database.sqlite.SQLiteDatabase .

关于java - 在 Android Content Provider 中执行 'like' 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33286302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com