问题
This is addendum to my question SQLiteDatabase Cursor empty only on Android 5.0+ devices. That one will be closed as I was not sure what is causing the bug - thought it was a cursor note being properly filled. Now I have discovered what is causing empty cursor.
Up to Android 5.0, search suggestions work by querying the database with the query:
SELECT rowid AS _id, suggest_text_1, suggest_text_2, rowid AS suggest_intent_data_id
FROM fts
WHERE (fts MATCH ?) //for example '*e*'
As of Android 5.0+, this does not work any more. I've done dozens of testings and the select query returns nothing to the Cursor
object if I set selectionArgs
to "*" + query + "*"
String selection = FTS3_TABLE + " MATCH ?";
String[] selectionArgs = new String[]{"*" + query + "*"};
On Android 5.0+ it will work ONLY if selectionArgs
are either query
or query + "*"
. If I put asterisk before the query
, it will stop working.
NOTE: I tested by querying a letter e
, not a special character.
Database querying looks like this and it worked flawlessly up to Android 5.0:
String[] columns = new String[]{
BaseColumns._ID,
KEY_QUESTION,
KEY_ANSWER,
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID};
String selection = FTS3_TABLE + " MATCH ?";
String[] selectionArgs = new String[]{"*" + query + "*"};
//mColumnMap
map.put(KEY_QUESTION, KEY_QUESTION);
map.put(KEY_ANSWER, KEY_ANSWER);
map.put(KEY_CARDID, KEY_CARDID);
map.put(KEY_CARDSET, KEY_CARDSET);
map.put(BaseColumns._ID, "rowid AS " +
BaseColumns._ID);
map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
//mColumnMap part ends
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(FTS3_TABLE);
builder.setProjectionMap(mColumnMap);
Cursor cursor = builder.query(db,
columns, selection, selectionArgs, null, null, null);
String temp = DatabaseUtils.dumpCursorToString(cursor); //empty string on Android 5.0+
As you can see, nothing complex or extraordinary.
Why I can no longer use query with asterisk on both sides of it?
What has changed as of Android 5.0 in this part?
回答1:
MATCH '*foo*'
queries never worked correctly in any version of sqlite. The fact that you got some results earlier was just a coincidence. Just the prefix form MATCH 'foo*'
(and MATCH 'foo'
) are supported.
Lollipop ships with a newer version of sqlite. For detailed list of changes between sqlite versions, see the changelog.
来源:https://stackoverflow.com/questions/30709715/match-with-selectargs-looking-like-queryterm-stopped-working-on-android-5-0