Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Autoincrement and server hostname in DB2

by ncw (Friar)
on Oct 06, 2000 at 23:27 UTC ( #35644=note: print w/replies, xml ) Need Help??


in reply to Autoincrement and server hostname in DB2

There isn't a DBI standard method of getting the id of a last autoincremented id as far as I know. I think this is because not all databases support this method of doing things.

That said I know how to do this in MySQL. After you have done

$cursor = $dbh->prepare("insert into table (a, b) values (?, ?)"); $cursor->execute($a_var, $b_var);
You can then read the last autoincrementid using a MySQL extension
$id = $dbh->{mysql_insertid};
This variable is related to your dbh session so it is safe in the presence of multiple updaters.

In a previous version of DBD::mysql the variable was just $dbh->{insertid}.

Don't try to get the last inserted id by "select max(id) from table" this will work short term, but get you into no end of trouble later. You could make it work by locking the table before your insert I guess.

It would be useful if this was standardised in DBI if possible. I personally build all my tables using autoincrementing id's and $dbh->{mysql_insertid} for joining them.

Look at your documentation for DBD::DB2 and see if it offers anything similar!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://35644]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-01-24 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?