Get auto-numbered primary key on insert?

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
thenickdude

Get auto-numbered primary key on insert?

Post by thenickdude »

Hey all,

I'm inserting a row into a table, the MySQL table has an AUTO_INCREMENT primary key. The problem is, I need to make an insertion into another table which references the first row I inserted, so I need to know what id was generated. How do you solve this? In PHP I'd just use mysql_insert_id().
Terence
Zeos Dev Team
Zeos Dev Team
Posts: 141
Joined: 22.09.2005, 14:11
Location: Stuttgart
Contact:

Post by Terence »

You can query for it with same params as for insert and "select id" afterwards , you can use mysql method in sql call "SELECT LAST_INSERT_ID()" or you can use TZUpdateSQL.OnAfterInsertStament Event for mysql only and only available for latest testing branch.

Also see here for detailed discusssion.
http://zeos.firmos.at/viewtopic.php?t=613&highlight=
fabian
thenickdude

Post by thenickdude »

SELECT LAST_INSERT_ID() sounds great, thanks.
Post Reply