【问题标题】:SQLilte Searching with prefix String by using MatchSQLite 使用 Match 搜索前缀字符串
【发布时间】:2013-02-22 09:23:32
【问题描述】:

我正在使用 FTS3 从数据库中获取内容,我需要获取带前缀的字符串(内容仅以该字符串开头)。但是当我使用 MATCH 时,它的行为就像包含。

这里的例子:

现在: 输入:123 输出:123abc 123aa aaa123

我想要什么: 输入:123 输出:123abc 123aaa

这里是我的代码:

db.rawQuery("SELECT * FROM " + SQLConstants.TABLE_NAME + " WHERE " + SQLConstants.TABLE_NAME + " MATCH '" + input + "*' ORDER BY " + CODE
        + " ASC;", null);

【问题讨论】:

  • 为我工作。您的数据是否真的包含“123aaa”?没有其他角色吗?

标签: android sqlite full-text-search android-sqlite


【解决方案1】:

对于像你这样的目标,通常使用LIKE 子句。

LIKE 运算符在 WHERE 子句中用于搜索指定的 列中的模式。

String value = "something";

解释:

like value + "%"; --> return everything that starts with value and behind it can be anything
like % + value; --> return everything that ends with value and before it can be anything
% + like value + "%"; --> return everything that contains value and before and behind it can be anything

在您的情况下,您需要第一种情况:

db.rawQuery("SELECT * FROM " + SQLConstants.TABLE_NAME 
         + " WHERE " + SQLConstants.COLUMN_NAME + " like ?
             ORDER BY " + CODE + " ASC", new String[] {input + "%"});

现在它可以工作了。

【讨论】:

  • 这个字符串中的 CODE 应该是什么?
【解决方案2】:

使用 LIKE 和 % 而不是 Match 和 *

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    相关资源
    最近更新 更多