Reputation: 33
I'm attempting to insert data to an SQLite database table and then immediately return the result from a select in the same call, but no data is returned.
Here is the SQL statement:
INSERT INTO Items (Name, DefaultExpirationIntervalId) VALUES ('Item', -1);
SELECT ItemId as '_id', Name, DefaultExpirationIntervalId FROM Items WHERE Name = 'Item';
Here is the code making the SQL call:
Boolean isReadOnly = getSQLiteDatabase().isReadOnly();
// Make sure we can add data to the database
if (!isReadOnly)
{
// Add the item to the database and then use the result to create a new item
Cursor cursor = getSQLiteDatabase().rawQuery(query, null);
if (null != cursor)
{
cursor.moveToFirst();
if (!cursor.isAfterLast())
item = new Item(cursor);
}
}
The rawQuery call returns a cursor (not null), but it is empty.
I verified through ADB that the record was inserted. I've even run this exact query through sqlite3 via the ADB shell and the item was inserted and returned as expected. I have also tried removing the select and retrieving the item in another rawQuery call and that didn't work either.
Does anybody have any idea what might be happening? I am running this code through a unit test that creates and and destroys the database during every test run.
Upvotes: 1
Views: 1512
Reputation: 38098
rawQuery is used ONLY for SELECT statements.
For INSERT, DELETE and UPDATE, you have to use execSQL instead
So, first you must make an INSERT by using execSQL AND THEN use rawQuery for your SELECT.
In case you're asking why...
a SELECT is a QUERY, while INSERT, DELETE and UPDATE are COMMANDS.
Upvotes: 2