On 9 Mar 2012, at 11:27am, Christoph P.U. Kukulies <[email protected]> wrote:

> I'm using System.Data.SQLite and the following problem comes up:
> 
> I'm doing an INSERT of some columns in a TABLE. I have to determine now if 
> the INSERT was successful. I'm doing this by putting the INSERT in a try { do 
> the insert...and try the UPDATE } catch { do nothing};
> 
> In case the INSERT fails because of a KEY clash the UPDATE isn't executed 
> either.
> But in case the INSERT is successful I would like to do the UPDATE right 
> afterwards on the same ROW that has been inserted last.
> You may argue, why I don't try the full INSERT right away. Reason: a large 
> BLOB has to be read inand the statement be prepared.
> And I could safe that, if the INSERT would fail anyway.
> 
> Is there something like UPDATE into last ROW that has  been inserted?

This function:

http://www.sqlite.org/c3ref/last_insert_rowid.html

will give you the rowid of the most recent row inserted.  So you could use it 
before your INSERT, then use it again after your INSERT.  If it didn't change, 
you know the INSERT failed.  And if it did change, you already have the value 
of the rowid you need for your UPDATE command.

If the System.Data.SQLite library doesn't implement this function, then you can 
access the same value using the "last_insert_rowid()" SQL function as 
documented here:

http://www.sqlite.org/lang_corefunc.html#last_insert_rowid

but this will of course, be slower.

Simon.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to