On 2 Nov 2012, at 8:58am, Ivan Shmakov <[email protected]> wrote:
> INSERT OR IGNORE INTO "foo" ("foo")
> VALUES (?1);
> INSERT INTO "bar" ("foo")
> VALUES ((SELECT f."rowid" FROM "foo" f WHERE f."foo" = ?1));
>
> Or is there a better way to ensure that the inner SELECT either
> returns a single row, or fails?
What do you mean by 'fails' ? Returning zero rows from a SELECT is not
failure: it's successfully reporting that there are no such rows in the table.
And there may be rows in the table even if the first INSERT failed: the rows
may have already been inserted.
If what you mean is that some syntax error or conflict prevented the first
INSERT from working, the correct way to do it is to look at the result returned
from that INSERT and see whether it is SQLITE_OK.
Howwever, if you just want to know whether there are any relevant rows in the
table ...
Solution 1: Before doing the second INSERT command do
SELECT count(*) FROM foo WHERE foo.foo = ?1
and look at the number it returns.
Solution 2: Call
sqlite3_last_insert_rowid(sqlite3*)
both before and after the first INSERT and see whether the value changes.
Simon.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users