Zay*_*ktu 5 delphi sqlite firemonkey
使用Firemonkey和SQLite的Delphi 10:运行下面的代码后,我想获取插入到SQLite表中的最后一条记录的ID.我如何获得最后一个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;
Run Code Online (Sandbox Code Playgroud)
如果您的ID列声明为INTEGER PRIMARY KEY,则可以查询last_insert_rowid.在这种情况下,该列将成为ROWID的别名.如果是这种情况,您可以通过以下方式本地查询:
uses
FireDAC.Phys.SQLiteWrapper;
function GetLastInsertRowID(Connection: TFDConnection): Int64;
begin
Result := Int64((TObject(Connection.CliObj) as TSQLiteDatabase).LastInsertRowid);
end;
Run Code Online (Sandbox Code Playgroud)
或者通过调用GetLastAutoGenValue方法以通用方式:
function GetLastInsertRowID(Connection: TFDConnection): Int64;
begin
Result := Int64(Connection.GetLastAutoGenValue(''));
end;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1381 次 |
最近记录: |