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

silvioprog silvioprog at gmail.com
Fri Nov 14 16:49:44 CET 2014


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? =/

E.g in a simple raw query in MySQL:

===============================================================================================
INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...);
SELECT LAST_INSERT_ID();

This will get you back the PRIMARY KEY value of the last row that *you*
 inserted:

The ID that was generated is maintained in the server on a *per-connection
basis*. This means that the value returned by the function to a given
client is the first AUTO_INCREMENT value generated for most recent
statement affecting an AUTO_INCREMENT column *by that client*.

So this is unaffected by other queries that might be running on the server
from other users.

S:
http://stackoverflow.com/questions/17112852/get-the-new-record-primary-key-id-from-mysql-insert-query
===============================================================================================

-- 
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/3ce024b3/attachment-0003.html>


More information about the Lazarus mailing list