How to get the ID of the last record inserted in SQLite with Delphi 10? How to get the ID of the last record inserted in SQLite with Delphi 10? sqlite sqlite

How to get the ID of the last record inserted in SQLite with Delphi 10?


You could query last_insert_rowid if your ID column is declared as INTEGER PRIMARY KEY. In such case the column becomes alias for the ROWID. If that is your case, you can query it natively e.g. this way:

uses  FireDAC.Phys.SQLiteWrapper;function GetLastInsertRowID(Connection: TFDConnection): Int64;begin  Result := Int64((TObject(Connection.CliObj) as TSQLiteDatabase).LastInsertRowid);end;

Or in common way by calling GetLastAutoGenValue method:

function GetLastInsertRowID(Connection: TFDConnection): Int64;begin  Result := Int64(Connection.GetLastAutoGenValue(''));end;