How to Get the Unique ID for the Last Inserted Row
How to Get the Unique ID for the Last Inserted Row
I've tried to get the last inserted row with MySQL function :
SELECT LAST_INSERT_ID()
But it always return 0.
Can I have an example with TSQLQuery?
Thanks for help!
SELECT LAST_INSERT_ID()
But it always return 0.
Can I have an example with TSQLQuery?
Thanks for help!
Use the following code:
Code: Select all
if SQLQuery.Active then
SQLQuery.Refresh
else
SQLQuery.Open;
Id := SQLQuery.Fields[0].AsInteger;
id is AUTO_INCREMENT
With DSQuery Do
Begin
SQLConnection := TmpSQLConnect;
SQL.Clear;
SQL.Add('INSERT INTO tclients SET id=null');
ExecSQL;
SQL.Clear;
SQL.Add('SELECT LAST_INSERT_ID()');
if DSQuery.Active then
DSQuery.Refresh
else
DSQuery.Open;
Result := DSQuery.Fields[0].AsInteger;
Close;
End;
still return 0
With DSQuery Do
Begin
SQLConnection := TmpSQLConnect;
SQL.Clear;
SQL.Add('INSERT INTO tclients SET id=null');
ExecSQL;
SQL.Clear;
SQL.Add('SELECT LAST_INSERT_ID()');
if DSQuery.Active then
DSQuery.Refresh
else
DSQuery.Open;
Result := DSQuery.Fields[0].AsInteger;
Close;
End;
still return 0
I still can not reproduce the problem.
Please send me a complete small sample to dmitryg*devart*com to demonstrate it, including a script to create and fill table.
Also supply me the following information:
- the exact version of DbxMda;
- the exact version of your IDE;
- the exact version of MySQL server.
Please send me a complete small sample to dmitryg*devart*com to demonstrate it, including a script to create and fill table.
Also supply me the following information:
- the exact version of DbxMda;
- the exact version of your IDE;
- the exact version of MySQL server.
This problem is connected with the specificity of MySQL client library work. To solve the problem use Direct mode to connect to a MySQL database. Change the following line for this:
to this line:
Code: Select all
NewSQLConnect.Params.Add('DriverName=MySQL');
Code: Select all
NewSQLConnect.Params.Add('DriverName=DevartMySQLDirect');