Android的SQLite的查询到包含文本列匹配 [英] Android sqlite query to match column containing text

查看:119
本文介绍了Android的SQLite的查询到包含文本列匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图进行查询,如果给定列包含的任何位置中的文本,将返回匹配。举例来说,如果我是通过单词真正包含文本的列这是一个很长的文本行将匹配。到目前为止,我查询仅返回是否完全匹配,因此将只匹配包含一列真,就是这样。

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

第一种方式(S为字符串,我传递):

 光标光标= mDb.query(DATABASE_TABLE,新的String [] {} KEY_TITLE,KEY_TITLE +LIKE'%+ S +%'COLLATE NOCASE,NULL,NULL, NULL,NULL);

下一页方式

 字符串p_query =SELECT COUNT(*)FROM数据WHERE数= COLLATE NOCASE?


解决方案

  

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


这对我的作品,它的参数:

 光标光标= mDb.query(DATABASE_TABLE,新的String [] {} KEY_TITLE,
        KEY_TITLE +怎么样?,新的String [] {%+ S +%},
        NULL,NULL,NULL);


真的是一个独特的足够的词,但如果你正在寻找的东西,如冰,不想骰子,风云,冰淇淋,​​等你将不得不使用不同的模式,如:%+ S +% S +%

或者使用FTS表与标记生成器,像波特词干。

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.

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

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

    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";

解决方案

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 + " %".

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

这篇关于Android的SQLite的查询到包含文本列匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆