TZQuery & LAST_INSERT_ID()

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

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
hetfield
Fresh Boarder
Fresh Boarder
Posts: 2
Joined: 22.09.2005, 10:35

TZQuery & LAST_INSERT_ID()

Post by hetfield »

Hi,

i am new to ZEOS components.

I use TZQuery with TDatasetProvider & TClientDataset to access a mysql-server 4.1.15 from BCB 6!

My test app is a simple master-detail-relation.

How can i get the autogenerated id after inserting the master record.

A simple "select LAST_INSERT_ID()" in TZQuery always return 0!

BDE's TQuery return the correct id of the master record.

I need the correct value to correct the corresponding master id in the detail record!

How can i get the right value?

Thx

hetfield
User avatar
fduenas
Zeos Dev Team
Zeos Dev Team
Posts: 132
Joined: 26.08.2005, 08:12
Location: Cancún

Post by fduenas »

Plz post always teh zeos version (i recommend the last one from CVS), delphi version also.

Are you sure you're posting the changes to the server?, make sure CachedUpdates is set to false.

I always use this method and don't have problems.

Just a TIP. When a master record is append and then posted, tzquery will refresh the content of the detail table, so you must:
1) unlink the detail table oin the OnBeforePost event of master table (Set detail's MasterdataSource property to nil),
2) get the last insert id,
3) replace the id key field in detail table from the first record to the last,
4) and in the OnAfterPostEvent of Master table link again the detail table with the master table.

I normally don't use master-detail relationship, but it needs that all records must be loaded in the Detail Dataset, I normally use the 'DataSource' property of TZQuery to link it to another table, normally this method is called Parametrized Queries.
Post Reply