Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to query SQLiteDatabase with non-string selection args?

Is there a straightforward way to query an SQLiteDatabase with selection args that are not String types?

Particularly: if the arg is a byte[] type?

The closest thing I can find is SQLiteDatabase.compileStatement(), which returns an SQLiteStatement you can call bindBlob etc. on. Unfortunately SQLiteStatement isn't good for querying because it doesn't return a cursor.

SQLiteCursor looks promising but I can't figure out how to use it.

like image 773
Steveo Avatar asked Jan 03 '14 20:01

Steveo


1 Answers

This is a design bug in the Android database API.

query and rawQuery accept only string parameters. execSQL accepts any Object parameters, but does not return results. SQLiteStatement accepts parameters of any type, but allows only queries that return a single value.

What you can do is to ignore parameters, and format the blobs as blob literals by hand:

cursor = db.rawQuery("SELECT a FROM b WHERE c = x'112233'", null);
like image 89
CL. Avatar answered Nov 15 '22 04:11

CL.