[Lazarus] TSQLQuery: Getting autoincremented ID value after insert (MySQL)

silvioprog silvioprog at gmail.com
Fri Nov 14 17:02:04 CET 2014


On Fri, Nov 14, 2014 at 12:49 PM, silvioprog <silvioprog at gmail.com> wrote:

> On Fri, Nov 14, 2014 at 11:43 AM, Michael Van Canneyt <
> michael at freepascal.org> wrote:
> [...]
>
>> AFAIK Only sqlite does not (maybe it does today). But that is not a DBMS
>> :-)
>
>
> Yes. It would be nice execute the "last_insert_rowid()" to return it in
> the same cursor (AFAIK, Android SDK use this mechanism in the
> nativeExecuteForLastInsertedRowId). So it can be same to the MySQL
> (LAST_INSERT_ID()). =)
>
> And you can use the TSQLite3Connection.getinsertid method, no? =/
>

It works fine in SQLite (my "test" table have ten records and "test2" one
record):

procedure TForm1.Button1Click(Sender: TObject);
var
  id1, id2: Int64;
begin
  SQLQuery1.Close;
  SQLQuery1.SQL.Text := 'insert into test (foo) values (''TEST'')';
  SQLQuery1.ExecSQL;
  id1 := SQLite3Connection1.GetInsertID;

  SQLQuery2.Close;
  SQLQuery2.SQL.Text := 'insert into test2 (foo) values (''TEST'');';
  SQLQuery2.ExecSQL;
  id2 := SQLite3Connection1.GetInsertID;

  ShowMessageFmt('ID1: %d; ID2: %d', [id1, id2]);
end;

[Window Title]
project1

[Content]
ID1: 11; ID2: 2

[OK]

I'll test it in MySQL ...

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20141114/f01095bc/attachment-0003.html>


More information about the Lazarus mailing list