Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Getting id of postgres record inserted via DBI

by cavac (Prior)
on Jun 22, 2023 at 06:43 UTC ( [id://11153082]=note: print w/replies, xml ) Need Help??


in reply to Getting id of postgres record inserted via DBI

Modern PostgreSQL supports the RETURNING keyword. Basically, you treat the statement as a one line SELECT afterwards.

my $sth = $dbh->prepare_cached("INSERT INTO bla (colname1, colname2) V +ALUES (?, ?) RETURNING bla_id") or croak($dbh->errstr); $sth->execute($val1, $val2) or croak($dbh->errstr); my $line = $sth->fetchrow_hashref; $sth->finish; $dbh->commit; print "Inserted data with new primary key ", $line->{bla_id}, "\n";

See also: https://www.postgresql.org/docs/current/dml-returning.html

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

Replies are listed 'Best First'.
Re^2: Getting id of postgres record inserted via DBI
by erix (Prior) on Jun 22, 2023 at 16:19 UTC

    Modern PostgreSQL supports the RETURNING keyword.

    s/Modern /Since time immemorial /;

    because INSERT ... RETURNING * is in PostgreSQL since 2006, version 8.2.

      For some people, 2006 tech is futuristic :P

        I agree that i'm old, yes. I started with PostgreSQL 7 or something like that (after having to work with Oracle for many years).

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-05-19 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.