Re: last insert id



Ron Savage wrote:
On Sun, 16 Apr 2006 22:15:22 +1000, Daniel Kasak wrote:

Hi Daniel

DBI: V 1.47
DBD::mysql: V 3.002

There are critical bugs in the $dbh->column_info() method in DBD::mysql-3.x that prevent us from upgrading :(
I've tried a line identical to your:

$id = $self -> dbh() -> last_insert_id(undef, undef, $table_name, undef);
and I didn't get anything useful from it. Ah well. 'select @@IDENTITY' works well for me, and appears to be the most compatible way of doing things.

Is last_insert_id() supposed to work in versions prior to 3.x? I know it's only a relatively recent addition. That means that the code below requires more checks on the version of DBD::mysql to see if last_insert_id() is supported ( and working ).
sub last_insert_id
{
my($self, $table_name) = @_;

my($id);

if ($self -> db_vendor() =~ /(?:mysql|Pg)/)
{
$id = $self -> dbh() -> last_insert_id(undef, undef, $table_name, undef);
}
else # Oracle.
{
my($sth) = $self -> dbh() -> prepare("select ${table_name}_id_seq.currval from dual");

$sth -> execute();

$id = ${$sth -> fetch()}[0];

$sth -> finish();
}

return $id;

} # End of last_insert_id.

.



Relevant Pages

  • Re: last insert id
    ... DBI: V 1.47 ... sub last_insert_id ... else # Oracle. ... $sth -> execute; ...
    (perl.dbi.users)
  • Re: Describe table
    ... Tim Bunce wrote: ... Oracle 8.1.5 ... DBI 1.42 ... but $sth is never defined when I use column_info-or primary_key_info- as you noticed above ...
    (perl.dbi.users)
  • Re: Bind Error - DBD::Oracle or DBI ???
    ... my $sth = $dbh->prepare(q( ... Using DBI 1.607 and DBD::Oracle 1.23 ... Fetch complete: 1 rows ... DBI::db=HASH) undef ...
    (perl.dbi.users)
  • Re: Bind Error - DBD::Oracle or DBI ???
    ... my $sth = $dbh->prepare(q( ... Using DBI 1.607 and DBD::Oracle 1.23 ... Fetch complete: 1 rows ... DBI::db=HASH) undef ...
    (perl.dbi.users)
  • selectall_arrayref/hashref error again
    ... I'm wondering about how to retrieve the error from bad sql passed to ... discover if the data is complete or was truncated due to an ... RaiseError without a $sth, it would be nice if the doc would just say ... I'm using perl 5.8.7 and DBI 1.50 ...
    (perl.dbi.users)