SQLiteDatabase Cursor empty only on Android 5.0+ devices

人走茶凉 提交于 2020-01-15 23:05:29

问题


The application has a SearchView which fetches suggestions from a specific database table. Everything worked without any errors until Android 5.0 appeared.

As of then, when the SQLiteQueryBuilder queries the database to fill the Cursor object, the return is empty cursor. Not NULL, but empty.

On other platforms, I can output the Cursor's content via DatabaseUtils.dumpCursorToString(cursorObject), but on Android 5.0+ the method reports output on null objects

Dumping cursor null
<<<<<

Even more: when I extract database file from 5.0+ devices and run the local SQL query, I can fetch all data. So the database is valid indeed. And the query is the simplest one

SELECT rowid AS _id, suggest_text_1, suggest_text_2, rowid AS suggest_intent_data_id 
FROM fts 
WHERE (fts MATCH '*e*') //<-- I pressed "e" on the keyboard

The logic for selecting data from the database and filling the Cursor object is really simple

    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(FTS3_TABLE);
    builder.setProjectionMap(mColumnMap);

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

I tried looking for some deprecated methods, but was without any luck.

I have already spent 3 days debugging each step in the process and I am out of any ideas what could be causing such behaviour.

Anyone has any ideas?

EDIT

The output of the method buildQuery()

String query = builder.buildQuery(columns, selection, null, null, null, null);

RESULT:   
SELECT rowid AS _id, suggest_text_1, suggest_text_2, rowid AS suggest_intent_data_id  
FROM fts   
WHERE (fts MATCH ?) 

selection and selectionArgs parameters are created like this

String selection = FTS3_TABLE + " MATCH ?";
String[] selectionArgs = new String[]{"*" + query + "*"};

The RAW query resulted the same thing as query via builder.query()

String query = builder.buildQuery(columns, selection, null, null, null, null);
Cursor temp = db.rawQuery(query, selectionArgs);
String output = DatabaseUtils.dumpCursorToString(temp);

Output: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@295023a7
        <<<<<

回答1:


Answered https://stackoverflow.com/a/30710226/437039 by laalto

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.




回答2:


Have you tried to query using the SQLiteDatabase query method like the following?

String selection = "fts" + " =? AND " +

 String[] selectionArgs = new String [] {
                "*e*"
        };
 String[] projection = new String [] { "rowId", "suggested_text"};
 SQLiteDatabase db = mDbHelper.getReadableDatabase();
 Cursor cursor = db.query("tableName",
                projection,selection, selectionArgs, null, null, null);


来源:https://stackoverflow.com/questions/30668938/sqlitedatabase-cursor-empty-only-on-android-5-0-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!