在Android上使用SQLite(与FTS) [英] using sqlite (with FTS) on Android

查看:101
本文介绍了在Android上使用SQLite(与FTS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的SQLite数据库FTS3,与我的Andr​​oid应用程序。

I am using sqlite FTS3 database, with my Android application.

   public Cursor getWordMatches(String query, String[] columns) {
        String selection = KEY_WORD + " MATCH ?";
        String[] selectionArgs = new String[] {query+"*"};
        return query(selection, selectionArgs, columns);
   }

......

.....

private Cursor query(String selection, String[] selectionArgs, String[] columns) {
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS_VIRTUAL_TABLE);
    builder.setProjectionMap(mColumnMap);     

    Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
            columns, selection, selectionArgs, null, null, null);

    if (cursor == null) {
        return null;
    } else if (!cursor.moveToFirst()) {
        cursor.close();
        return null;
    }
    return cursor;
}

如果我做一个查询和搜索'家',我会得到:

If I make a query and search for 'house' I will get:

household 
house 
apartment house 
big house

(我得到的一切,有房子在里面,在正则表达式的房子的)

我怎样才能得到这种结果对于我的查询?

How can I get this kind of result for my query?

household
house

(所以我只想要结果入手的房子,在房子的正则表达式*)

(So i only want results that start with house, in regex house*)

我的Andr​​oid应用程序FORCE关闭如我使用LIKE语句(也许是因为数据库有20万行)。 所以,我应该以某种方式结合MATCH和LIKE语句? 它甚至有可能做到这一点在Android上,如果不是,也许我应该尝试在光标运行一些正则表达式后,我从该查询得到它?

My android application FORCE CLOSES if I use LIKE statement(maybe because db has 200000 rows). So should I somehow combine MATCH and LIKE statement? Is it even possible to do this on Android, if it is not maybe I should try to run some regex on cursor after I get it from this query?

推荐答案

全文检索表应该总是使用火柴不喜欢,因为LIKE查询执行表的完全扫描这违背在创建FTS表的目的位居第一。

Full text search tables should always use MATCH and not LIKE, because a LIKE query performs a full scan of the table which defeats the purpose of creating the FTS table in the first place.

要解决问题,创建FTS表时使用的标记生成非默认尝试:

To solve your problem, trying using a tokenizer other than the default when creating your fts table:

-- Create a table using the simple tokenizer.
CREATE VIRTUAL TABLE simple USING fts3(tokenize=porter);

在默认情况下,SQLite的使用一个简单的标记生成器可产生你不想要的比赛。切换到门房标记生成器,看看会发生什么。最糟糕的情况下,可以实现自定义的标记生成器。参见更多信息的 SQLite的文档

这篇关于在Android上使用SQLite(与FTS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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