SQLiteDatabase Cursor empty only on Android 5.0+ devices - android

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

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.

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

Related

How to fix sqlite query error where the ? are not replaced by the selection arguments

My app is a dictionary and now I am working on making it available offline. The database is downloaded and now we have the situation that there is no internet. I need to use the local database. I get my readable database, and then set up a query. On my own testing phone everything works fine. But now before publishing the update I was trying it on two other phones, just to see that the ? in the selection are not replaced by the values in the selectionArgs.
The debugger just shows that the sql statement was built without replacing anything in the selection.
It works on my phone on Android 8.1, but with the two phone that are lower, it stops working and I am out of ideas.
I also tried changing it to db.rawQuery() but that ended with the same result.
String selection = "eng LIKE ? OR oky LIKE ? OR engpl LIKE ? OR okypl LIKE ? OR engcom LIKE ? OR okycom LIKE ? OR Alternative LIKE ?";
String[] selectionArgs = {term, term, term, term, com, com, term};
String orderBy = "eng, oky ASC";
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null, orderBy);
Now I expected the result to give me the terms that fit to the word (term) that was searched for by the user, but instead the cursor is empty, because there is no term containing a question mark.
Especially confusing is that it works on one phone and not on others.
The debugger won't show the resolved ?'s as the replacement of the ?'s is done by the underlying SDK (sqlite implementation) which is in C rather than java.
The best that you can see are the bindArgs e.g. for the following code :-
csr = mDBhlpr.getWritableDatabase().query("table1",null,"id=?",new String[]{"1"},null,null,null);
while (csr.moveToNext()) {
long id = csr.getLong(csr.getColumnIndex(DBHelper.COL_TABLE1_ID));
}
with a breakpoint at the 1st line then you could get :-
I don't believe that the issue is with the binding of the arguments, rather that it is elsewhere. You could test this by changing :-
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null, orderBy);
To
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, null, null, null, null, null);
DatabaseUtils.dumpCursor(cursor);
i.e. select all rows, and them write all the rows in the Cursor to the Log.
If still none then the tables on the other devices are empty.
If an Exception occurs then you would have to investigate that.
If this works as expected, then progressively (one by one) add the selection criteria and args.
e.g. first would be
String selection = "eng LIKE ?";
String[] selectionArgs = {term};
String orderBy = "eng, oky ASC";
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null, null);
then :-
String selection = "eng LIKE ? OR oky LIKE ?";
String[] selectionArgs = {term, term};
String orderBy = "eng, oky ASC";
Cursor cursor = db.query(DictionaryContract.WordEntry.TABLE_NAME, null, selection, selectionArgs, null, null,null);
and so on (note finally re-add the order by).
If none of the above works, then your issue (as expected) is elsewhere.

Android SQLite query cursor moveToFirst

I'm making an Android App with a sqlite db, which has a query method like this:
String[] projection = {COLUMN_NAME_ID};
String selection = COLUMN_NAME_TEAM + " = ?";
String[] selectionArgs = {teamId.toString()};
String sortOrder = COLUMN_NAME_NAME + " ASC";
Cursor c = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
if (c.moveToFirst()){
//...
}
as suggested here. The point is to implement this query:
SELECT PLAYER_ID FROM PLAYERS WHERE TEAM_ID = ? ORDER BY NAME ASC;
According to the SQLiteDatabase.query documentation, this is a valid call to query(), and so far I've been doing it successfully with all my other queries in this App.
For some reason, in this query (and only in this) the App freezes in c.moveToFirst().
Some answers to similar questions suggest that it might be a performance issue. I don't think this is the case, as my table has only 6 rows and the query is quite simple.
Any ideas?
Thanks

How to use selection args to query specific rows from a contentprovider in android

i have constructed a basic content provider that stores SMS messages for learning purposes, so far i can read(without selection args), insert, update and delete.
However i have been stumped trying to figure out how to format the selection args for the WHERE clause in my provider:
Basicly my application needs to search for a specific timestamp (in long format) and return its _id
say your database has an entry like this that your trying to access:
2|1|1410293471300|test type 1||testing|0
and the entire database looks like this:
_id|CLIENTTRXID|CREATED_AT|TYPE|MESSAGEPRIO|MESSAGE|ACCEPTED
1|1|1410293471000|test type 1||testing|0
2|1|1410293471300|test type 1||testing|0
3|1|1410293471600|test type 1||testing|0
in sql the query would be
"select _id from alerts where CREATED_AT=1410293471300;"
the code i was hoping would do the equivalent:
//normally i would get the string dynamically but to make it equal to the sql
String date = "1410293471300";
String[] selectionArgs = new String[]{ date };
Cursor cursor = getContext().getContentResolver().query(AlertContract.CONTENT_URI, null, AlertContract.Column.CREATED_AT, selectionArgs, AlertContract.DEFAULT_SORT);
seems to always produce the following error no matter what i try as selectionArgs
Exception caught﹕ Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
here is the query method of my contentprovider:
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables( AlertContract.TABLE);
switch (sURIMatcher.match(uri)) {
case AlertContract.STATUS_DIR:
break;
case AlertContract.STATUS_ITEM:
qb.appendWhere(AlertContract.Column.ID + "=" + uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException( "illegal uri: " + uri);
}
String orderBy = (TextUtils.isEmpty(sortOrder)) ? AlertContract.DEFAULT_SORT : sortOrder;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
//register for uri changes
cursor.setNotificationUri(getContext().getContentResolver(), uri);
Log.d(TAG, "queried records: "+cursor.getCount());
return cursor;
}
Presumably im missing something extremely obvious, and will feel quite silly for having posted this question.
But for the moment i would very much appreciate any help, as i am quite stumped.
It looks like your issue is with your selection, rather than with your selectionArgs per se. The selection should be the whole query after the "where". Here your selection is "CREATED_AT". You need two more items to get it to work:
an =, since you want equality (you can also do other operators, of course)
a ?. This is where your selectionArgument will be inserted (each argument needs a ? in the selection, so there should be the same number of ?s in the selection as selectionArguments.
The end result should be more like "CREATED_AT = ?"
Check out the documentation and this tutorial for more info on how to correctly construct a ContentProvider query.
When you query the content provider, try the following. The selection should be AlertContract.Column.CREATED_AT + "=?"
Cursor cursor = getContext().getContentResolver().query(AlertContract.CONTENT_URI, null, AlertContract.Column.CREATED_AT + "=?", selectionArgs, AlertContract.DEFAULT_SORT);

Understanding SQLite Cursor Behavior

I'm writing a method to update default settings in a table. The table is very simple: two columns, the first containing labels to indicate the type of setting, the second to store the value of the setting.
At this point in the execution, the table is empty. I'm just setting up the initial value. So, I expect that this cursor will come back empty. But instead, I'm getting an error (shown below). The setting that I am working with is called "lastPlayer" and is supposed to get stored in the "SETTING_COLUMN" in the "SETTINGS_TABLE". Here's the code:
public static void updateSetting(String setting, String newVal) {
String table = "SETTINGS_TABLE";
String[] resultColumn = new String[] {VALUE_COLUMN};
String where = SETTING_COLUMN + "=" + setting;
System.err.println(where);
SQLiteDatabase db = godSimDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(table, resultColumn, where, null, null, null, null);
System.err.println("cursor returned"); //I never see this ouput
\\more
}
sqlite returned: error code = 1, msg = no such column: lastPlayer
Why is it saying that there is no such column lastPlayer? I thought that I was telling the query to look at the column "SETTING_COLUMN" and return the record where that column has a value "lastPlayer". I'm confused. Can somebody straighten me out? I've been looking a this for an hour and I just don't see what I am doing wrong.
Thanks!
You're not properly building/escaping your query. Since the value lastPlayer is not in quotes, your statement is checking for equality of two columns, which is what that error message is saying.
To properly build your query, it's best to not do this manually with String concatenation. Instead, the parameter selectionArgs of SQLiteDatabase.query() is meant to do this.
The parameters in your query should be defined as ? and then filled in based on the selectionArgs. From the docs:
You may include ?s in selection, which will be replaced by the values
from selectionArgs, in order that they appear in the selection. The
values will be bound as Strings.
So, your code would look like this:
String where = SETTING_COLUMN + " = ?";
Cursor cursor = db.query(table, resultColumn, where, new String[] { setting }, null, null, null);

Android sql query using WHERE clause

Dear Stack Overflow Community,
I have a question regarding how to incorporate a WHERE clause when querying a sql database in Android. My goal is to return specific records from my database where the date picked by a datepicker matches the date stored.
Here is my code:
private static String datePickerDate = "3/29/2011";
private static String[] FROM = { _ID, NAME, PRICE, DATE, TIME };
private static String ORDER_BY = TIME + " DESC ";
private static String WHERE = DATE + "is like " + datePickerDate;
private Cursor getEvents(){
// Perform a managed Query. The Activity will handle closing
// and re-querying the cursor when needed
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}
My questions are thus:
Where does my "WHERE" statement go?
And is my "WHERE" statement correct?
The documentation I found for db.query doesn't specify if a "WHERE" clause can be used, it just specifies that a "HAVING" clause is possible, but I don't quite think that's what I'm wanting.
db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
The selection represents your WHERE clause.
From the doc
selection A filter declaring which
rows to return, formatted as an SQL
WHERE clause (excluding the WHERE
itself). Passing null will return all
rows for the given table.
So you can try this (untested):
private static String WHERE = "DATE like ?";
db.query(table, columns, WHERE , new String[] { datePickerDate }, groupBy, having, orderBy)
If you want to use the WHERE clause, I would suggest using the raw query function in the SQLiteDatabase class, shown here.
This way, you can have the raw query typed out (or in segments) as if you were doing it naturally with SQL.
EDIT: On a side note, the "selection" parameter of the query() function corresponds to the WHERE clause, as noted here
About where you're "WHERE" goes - look here
And regarding the second part, I think you miss the qouts before and after the value, it should be .. like '3/29/2011' and not .. is like 3/29/2011

Categories

Resources