tags:

views:

105

answers:

2

Is there an sqlite equivalent to INSERT...;SELECT @@IDENTITY? If so, please show me the code or point me towards it.

Thanks!

+1  A: 

Cursor.lastrowid

>>> import sqlite3
>>> conn = sqlite3.connect(":memory:")
>>> c = conn.cursor()
>>> c.execute("create table t (id integer, some text);")
<sqlite3.Cursor object at 0x00C64CE0>
>>> c.execute("insert into t values(1,'a');")
<sqlite3.Cursor object at 0x00C64CE0>
>>> c.lastrowid
1
Adam Bernier
Thanks! This example worked perfectly for what I needed. I didn't try the other answer, but assume it would have worked too.
javovo
+1  A: 
SELECT last_insert_rowid() -- same as select @@identity

last_insert_rowid() The last_insert_rowid() function returns the ROWID of the last row insert from the database connection which invoked the function. The last_insert_rowid() SQL function is a wrapper around the sqlite3_last_insert_rowid() C/C++ interface function.

Pranay Rana