I\'ve been looking at the official documentation (http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html) and cross-referencing with StackOverflo
You are right. The documentation is confusing. Overall, the design attempts to provide a convenient Java wrapper around the sqlite3 C API. For most of the time it works fine if you use it the way the designers intended, e.g. using the convenience methods for CRUD operations. But they also needed to provide raw query methods execSQL()
and rawQuery()
for cases where the convenience CRUD methods are not powerful enough or not applicable at all (CREATE TABLE
and so on). This causes leaky abstractions.
The doc says for
rawQuery
that statements must not be terminated by a semicolon, but it doesn't actually seem to make any difference. This matters to me because we have a huge XML document filled with queries I use in my app across several platforms and I'd prefer to keep it identical if possible.
The docs are bad. In fact, Android SQLiteDatabase
itself calls rawQuery with a semicolon-terminated query string.
rawQuery
doesn't seem to work for inserts (yes, I've tried with and without the semicolon). The doc don't say anything about this. I do see that it returns a Cursor, which I suppose could be an oblique hint that it only works with a select, but not necessarily -- it could simply return a zero-length or null Cursor when they was no result set.
It does work but you'll need to understand how it works on native level.
Think of execSQL()
as sqlite3_exec() that runs the query and returns either success or an error code.
Think of rawQuery()
as sqlite3_prepare() that compiles the query but does not run it yet. To actually run it, use one of the moveTo...()
methods on the Cursor
. Think of this as sqlite3_step(). Combining any rawQuery()
with moveTo...()
will actually alter the database.
execSQL(String sql, Object[] bindArgs)
explicitly says that it does not work with selects, but in fact it does!Furthermore, although
execSQL(String, Object[])
specifically tells you not to try CRUD operations, its parameterless version contains no such warning, and also works fine for that purpose (with the disadvantage of not allowing SQL parameters).
It works fine for all CRUD operations. For the R read part of CRUD, there's just no way to get the selected data.
What you want is execSQL() for inserts. Or you can do it this way:
ContentValues values = new ContentValues();
values.put ("col1", "foo");
values.put ("cols2", "bar");
getDb().insertOrThrow ("MyTable", null, values);
For queries, you can use rawQuery.