Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, all I'm trying to use class::dbi through ODBC drivers to a M$ Access DB. However when I use the find_or_create function to insert a row to a table(the fields are quite simple as (id,name) where id is the primary key), without set the primary key, the code fails, saying that Can't get last insert id...

I trace the src code to the Class::DBI, it is actually from the following method:

sub _auto_increment_value { my $self = shift; my $dbh = $self->db_Main; # the DBI will provide a standard attribute soon, meanwhile... my $id = $dbh->{mysql_insertid} # mysql || eval { $dbh->func('last_insert_rowid') }; # SQLite $self->_croak("Can't get last insert id") unless defined $id; return $id; }
Does this limit the Class::DBI usage? For those DBD other than mysql, how should this be solved? Thanks in advance for any help.

Replies are listed 'Best First'.
Re: Class::DBI vs DBD::ODBC
by dbwiz (Curate) on Sep 23, 2003 at 13:00 UTC

    The latest release of DBI introduced a new method to get the last insert id.

    See the announcement in the Changelog.

    However, the DBI documentation warns that this method is heavily influenced by driver implementation.

    Check out this piece of advice from the DBI docs. It may be able to meet your needs. I didn't test it.

    * Some drivers may need driver-specific hints about how to get the value. For example, being told the name of the database 'sequence' object that holds the value. Any such hints are passed as driver-specific attributes in the \%attr parameter.