Delphi10withFire猴子和SQLite:在运行下面的代码之后,我希望获得插入到SQLite表中的最后一个记录的ID。我怎么才能拿到最后一个身份证?
注意:表1的ID字段是自动增量。
var myQr: TFDQuery;
begin
myQr := TFDQuery.Create(Self);
with myQr do begin
SQL.Add('Insert into table1 values (:_id, :_name, :_dthr)');
Params.ParamByName('_id').ParamType := TParamType.ptInput;
Params.ParamByName('_id').DataType := TFieldType.ftInteger;
Params.ParamByName('_id').Value := null;
ParamByName('_name').AsString := 'name test';
ParamByName('_dthr').AsDateTime := Now;
ExecSQL;
end;
// How to get last ID? <<<<<<<<<<<<<=================
myQr.DisposeOf;发布于 2018-03-22 20:17:43
如果您的last ID列声明为INTEGER PRIMARY KEY,则可以查询INTEGER PRIMARY KEY。在这种情况下,列成为ROWID的别名。如果这是您的情况,您可以以本地方式进行查询,例如:
uses
FireDAC.Phys.SQLiteWrapper;
function GetLastInsertRowID(Connection: TFDConnection): Int64;
begin
Result := Int64((TObject(Connection.CliObj) as TSQLiteDatabase).LastInsertRowid);
end;或者通过调用GetLastAutoGenValue方法,以常见的方式:
function GetLastInsertRowID(Connection: TFDConnection): Int64;
begin
Result := Int64(Connection.GetLastAutoGenValue(''));
end;https://stackoverflow.com/questions/49437453
复制相似问题