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
|
---|
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 | |
by marto (Cardinal) on Jun 22, 2023 at 16:21 UTC | |
by cavac (Prior) on Jun 26, 2023 at 08:26 UTC | |
by erix (Prior) on Jul 04, 2023 at 09:11 UTC | |
by Tux (Canon) on Jul 04, 2023 at 12:01 UTC |