Retrieve last Autogenerated Record Key

Perhaps I am making something difficult when I shouldn’t. I have a SQLite database table with a column as “pkRecID INTEGER PRIMARY KEY”. I have a situation where I add a new record to the table that is really just a skeleton with a couple of primary columns entered. Right after I add the new record I need retrieve it but I don’t yet know what the new pkRecID is (that’s actually why I am retrieving it).

Is there a way to just recall the last record added, or retrieve MAX(pkRecID) or something?

thanks,
bill

SELECT last_insert_rowid() AS LASTID

Or you can use SQLiteDatabase.LastRowID.

– Ex1:

BEGIN;
INSERT INTO master (Name) VALUES (‘HEADER’);
INSERT INTO detail (MasterID, Name) VALUES (last_insert_rowid(),’Article one’);
COMMIT;

– Ex2:

BEGIN;
INSERT INTO VV (Name, V1, V2) VALUES (‘DATA’, 2, 3);
SELECT Name, (V1+V2) AS VALS FROM VV WHERE ROWID=last_insert_rowid();
COMMIT;

Thanks Paul, I think your solution is the most straightforward for what I need to do.

Thanks to Rick also but I just like Paul’s way.

thanks to both,
bill