[Gambas-user] gb.db autoincrement ?

admin at ...1080... admin at ...1080...
Fri Oct 17 19:04:51 CEST 2008


Hi Doriano

With transactions there are many different lock levels, look at help.

But in case you are using mySQL there is the function LAST_INSERT_ID (),
see: 
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_last-insert-id.

Other vendors maybe have also functions or variables to catch the last 
inserted id.


Regards
Werner(007)



--- snipp ---
I never tried transactions: could you be more explicit with an example
(pseudo code)?

There are two problems. First, how to obtain the correct ID
autoincrement field inserted in the master table; I think this is done
through a select, like I did. Second, how to prevent another record be
inserted in the master table, by another process, before doing the
query. I am not sure that a transaction prevents this, but if you are,
tell me it is so.
Last time you talked about this, you speaked about locks, not
transactions. Using transaction it could go like:

begin transaction
add record to invoice table (where invoices.ID is serial/autoincrement)
query database to know what the last ID number is
add records to items table, using this known invoices.ID as foreign key
terminate transaction

But, if I well remember, transaction are meant to update database in a
"all done, or nothing done" fashion, which means in this case that the
invoice, and all the children items, are added to the database but: if
something goes wrong, then nothing is added. This is not the same as to
say "the invoices table is locked, and only the active transaction can
update it". Hope I am clear enough.
If you know that transactions solve problems, I trust you.

Regards,
Doriano

--- snipp ---





More information about the User mailing list