Herman's solution worked for me, but ... I got mixed up a bit. I included the demo I was working on based on his answer. Additional features in my answer include foreign key support, auto-increment keys, and using the last_insert_rowid() function to get the last automatically generated key in a transaction.
My need for this information arose when I came across a transaction that required three foreign keys, but I could only get the last with last_insert_rowid() .
PRAGMA foreign_keys = ON; -- sqlite foreign key support is off by default PRAGMA temp_store = 2; -- store temp table in memory, not on disk CREATE TABLE Foo( Thing1 INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ); CREATE TABLE Bar( Thing2 INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, FOREIGN KEY(Thing2) REFERENCES Foo(Thing1) ); BEGIN TRANSACTION; CREATE TEMP TABLE _Variables(Key TEXT, Value INTEGER); INSERT INTO Foo(Thing1) VALUES(2); INSERT INTO _Variables(Key, Value) VALUES('FooThing', last_insert_rowid()); INSERT INTO Bar(Thing2) VALUES((SELECT Value FROM _Variables WHERE Key = 'FooThing')); DROP TABLE _Variables; END TRANSACTION;
ThisClark Jan 08 '16 at 6:54 on 2016-01-08 06:54
source share