java Android sqlite 查询以匹配包含文本的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16022427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:39:18  来源:igfitidea点击:

Android sqlite query to match column containing text

javaandroidsqlite

提问by Paul

Trying to make a query that will return a match if the given column contains the text anywhere inside. For instance if I were to pass the word "really" a column containing the text "This is a really long line of text" would match. So far my queries only return if it matches exactly so it would only match a column containing "really" and that is it.

如果给定的列包含其中任何位置的文本,则尝试进行将返回匹配项的查询。例如,如果我要传递单词“really”,则包含文本“This is a real long line of text”的列将匹配。到目前为止,我的查询仅在完全匹配时返回,因此它只会匹配包含“really”的列,仅此而已。

I've tried a couple ways but both seem to return the same result:

我尝试了几种方法,但似乎都返回了相同的结果:

First way (s is the string I'm passing in):

第一种方式(s 是我传入的字符串):

    Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_TITLE}, KEY_TITLE + " LIKE '%" + s + "%' COLLATE NOCASE", null, null, null, null);

Next way

下一条路

String p_query = "SELECT COUNT(*) FROM data WHERE number=? COLLATE NOCASE";

回答by Sam

Trying to make a query that will return a match if the given column contains the text anywhere inside.

如果给定的列包含其中任何位置的文本,则尝试进行将返回匹配项的查询。

This works for me and it's parameterized:

这对我有用并且它是参数化的:

Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_TITLE}, 
        KEY_TITLE + " LIKE ?", new String[] {"%" + s + "%"}, 
        null, null, null);


"Really" is a distinct enough word, but if you are searching for something like "ice" and don't want "dice", "vice", "icecream", etc you'll have to use different patterns like: "% " + s + " %"and s + " %".

“Really”是一个足够独特的词,但是如果您正在搜索诸如“ice”之类的东西并且不想要“dice”、“vice”、“icecream”等,您将不得不使用不同的模式,例如:"% " + s + " %"s + " %"

Or use an FTS table with a tokenizer, like Porter Stemming.

或者使用带有标记器的 FTS 表,例如 Porter Stemming。