3

I have an insert or replace command similar to this:

INSERT or REPLACE INTO author(id,bookId,name) values((SELECT id FROM author WHERE bookId='4'),'4','Book Title')

Basically, it replaces a row if an id exists that aligns with a a bookId. If there is no id it inserts.

I'm using an application called "SQLite Database Browser 2.0" to test my SQLite commands on the same database tables I have in my app (I'm exporting the database from the emulator). The above command works like a charm in the Database Browser but the app doesn't insert or replace (right now it should insert since the table is empty).

I know my SQLite command is correct and I don't get any errors in my Logcat.

Am I missing a piece Android needs to do this command?

dcp3450
  • 10,091
  • 20
  • 52
  • 96
  • is it inserting the value if bookId not exists .? – Vipin Sahu Dec 06 '13 at 09:15
  • No, it's not inserting. I exported the database from the emulator and literally ran the same SQLite statement the app runs and it works. (I'm logging each statement to the logcat). – dcp3450 Dec 06 '13 at 09:20
  • Which code you use to execute this, `execSQL()` or `rawQuery()`? Or perhaps can you post the snippet to insert your data in Android? (Also, look [this](http://stackoverflow.com/questions/12224964/android-sqlite-insert-or-replace?rq=1)) – Andrew T. Dec 06 '13 at 09:24
  • if it is not giving error then data must be inserted , are you sure that u have used beginTranstion and committed it . – Vipin Sahu Dec 06 '13 at 09:26
  • @AndrewT. - I'm using rawQuery. Changed it to execSQL and it worked. Can you provide this as the answer so I can select it? – dcp3450 Dec 06 '13 at 09:30
  • laalto has answered it, although I'm still not sure the reason. `rawQuery()` still executes the query if it is `SELECT` statement, but based on many experiences, for non-query statement, `execSQL()` can do it instead of `rawQuery()`. – Andrew T. Dec 06 '13 at 15:51

1 Answers1

0

Based on the question comments, the problem is that rawQuery() just compiles the SQL you pass in but does not execute it. To execute SQL compiled with rawQuery(), you generally would issue one of the moveTo...() calls on the returned Cursor.

In this case, to both compile and execute the SQL, use execSQL().

laalto
  • 137,703
  • 64
  • 254
  • 280