[sf-perl] PostgreSQL DBD ---- repost: sorry, I had the wrong subject in my previous post

Dan Lyke danlyke at flutterby.com
Fri Apr 1 08:10:56 PST 2005


David Fetter writes:
>     SELECT currval('the_name_of_the_appropriate_seq');
> 
> Not quite as convenient, and I've got a query in to the DBD::Pg dev
> team, but it will work.

My "Oh crap I'm going to be cleaning that code up someday" filter just
kicked in...

I'm a really strong proponent of wrapping insert statements in a
function which takes a tablename and a set of key-value pairs, and
returns an ID. For MySQL and SQLite I do the insert and then get the
last insert ID, for PostgreSQL I do a:

   SELECT NEXTVAL('table_id_seq')

And insert with that value.

If you have multiple users of the database (ala, say, a web app),
doing the

   SELECT CURRVAL('table_id_seq')

allows for another insert to sneak in between your insert and the
select.




More information about the SanFrancisco-pm mailing list