如何在Android中使用sqlite与glob [英] how to use glob with sqlite in android
问题描述
我有一个23,000个单词的数据库.我想让查询返回以某个搜索字符串开头的所有单词.在阅读 android中的sqlite LIKE问题之后,我可以使用LIKE来获得有效的查询.在这里:
I have a database of 23,000 words. I want to have the query return all words that start with a certain search string. After reading sqlite LIKE problem in android I was able to get a working query using LIKE. Here it is:
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = { KEY_WORD };
String sqlTables = TABLE_WORDS;
//String selection = KEY_FUZZYWORD + " =?"; // for an exact match
String selection = KEY_FUZZYWORD + " LIKE ?";
String[] selectionArgs = { searchString + "%" };
String limit = Integer.toString(WORD_QUERY_LIMIT);
qb.setTables(sqlTables);
Cursor cursor = qb.query(db, sqlSelect, selection, selectionArgs, null, null, null, limit);
关键是
String selection = KEY_FUZZYWORD + " LIKE ?";
String[] selectionArgs = { searchString + "%" };
但是我还阅读了>为什么需要GLOB子句在SQLite中使用?区分大小写适合我的情况,所以我想尝试一下.如何使用带有selectionArgs的SQLiteQueryBuilder进行设置?我很难找到一个例子. (我不想使用rawQuery以避免SQL注入.)
But I also read Why do we need the GLOB clause in SQLite? Case sensitivity is fine for my situation so I thought I would try it. How do you set this up using SQLiteQueryBuilder with selectionArgs? I was having a lot of trouble finding an example. (I don't want to use a rawQuery in order to avoid SQL injection.)
推荐答案
它与您已经拥有的非常相似.
It is very similar to what you have already.
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = { KEY_WORD };
String sqlTables = TABLE_WORDS;
String selection = KEY_FUZZYWORD + " GLOB ?";
String[] selectionArgs = { searchString + "*" };
String limit = Integer.toString(WORD_QUERY_LIMIT);
qb.setTables(sqlTables);
Cursor cursor = qb.query(db, sqlSelect, selection, selectionArgs, null, null, null, limit);
关键所在
String selection = KEY_FUZZYWORD + " GLOB ?";
String[] selectionArgs = { searchString + "*" };
进一步阅读:
- SQLite-GLOB子句
- SQLite理解的SQL (向下滚动至"LIKE和GLOB运算符")
- SQLite - GLOB Clause
- SQL As Understood By SQLite (scroll down to "The LIKE and GLOB operators")
这篇关于如何在Android中使用sqlite与glob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!