I am using sqlite with python. When i insert into table A i need to feed it an ID from table B. So what i wanted to do is insert default data into B, grab the id (which is auto increment) and use it in table A. Whats the best way receive the key from the table i just inserted into?
Share
As Christian said,
sqlite3_last_insert_rowid()is what you want… but that’s the C level API, and you’re using the Python DB-API bindings for SQLite.It looks like the cursor method
lastrowidwill do what you want (search for ‘lastrowid’ in the documentation for more information). Insert your row withcursor.execute( ... ), then do something likelastid = cursor.lastrowidto check the last ID inserted.That you say you need ‘an’ ID worries me, though… it doesn’t matter which ID you have? Unless you are using the data just inserted into B for something, in which case you need that row ID, your database structure is seriously screwed up if you just need any old row ID for table B.