MySQL Forums
Forum List  »  Perl

$dbh->last_insert_id support
Posted by: Jay Buffington
Date: January 11, 2007 01:39PM

Hello,

What is the status of support for DBI's last_insert_id() method? The DBI documentation talks about MySQL and this method in its POD, http://search.cpan.org/~timb/DBI-1.53/DBI.pm#Database_Handle_Methods

The DBD::mysql docs don't mention the last_insert_id method at all. Also, there are no tests for it in the package. I added a test for 4.00 and it failed for me:

jay@webdev:~$ diff -u DBD-mysql-4.00/t/insertid.t DBD-mysql-4.00-patched/t/insertid.t
--- DBD-mysql-4.00/t/insertid.t 2006-12-23 09:54:54.000000000 -0800
+++ DBD-mysql-4.00-patched/t/insertid.t 2007-01-11 10:31:06.000000000 -0800
@@ -40,6 +40,9 @@
# Verify $dbh->insertid
Test($state or ($dbh->{'mysql_insertid'} eq "1"));

+ # Test last_insert_id()
+ Test($state or ($dbh->last_insert_id(undef,undef,undef,undef) eq "1"));
+
#
# Insert another row
#


I also tried this script on the same system but with DBD::mysql 2.9006:
jay@webdev:~$ cat t.pl
#!/usr/bin/perl

use warnings;
use strict;

use DBD::mysql;

my $dsn = "DBI:mysql:host=sips.imagestation.com;port=3306";
my $user = 'root';
my $password = 'xxx';

my $dbh = DBI->connect($dsn, $user, $password);

# CREATE TABLE `test_last_insert_id` (
# `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
# `greeting` VARCHAR( 32 ) NOT NULL ,
# PRIMARY KEY ( `id` )
# ) TYPE = MYISAM ;

$dbh->do( "insert into test.test_last_insert_id (greeting) values ('hello')" ) or die $dbh->errstr();
my $row = $dbh->selectrow_arrayref("select LAST_INSERT_ID()") or die $dbh->errstr();
print "Database says LAST_INSERT_ID is " . $row->[0] . "\n";

my $id = $dbh->last_insert_id( undef, undef, undef, undef ) or die "Couldn't get last insert id: " . $dbh->errstr();
print "Inserted row with id $id\n";

jay@webdev:~$ perl t.pl
Database says LAST_INSERT_ID is 15
Use of uninitialized value in concatenation (.) or string at t.pl line 24.
Couldn't get last insert id: at t.pl line 24.

Is last_insert_id supported? Or should I try something else?

Thanks,
Jay

Options: ReplyQuote


Subject
Written By
Posted
$dbh->last_insert_id support
January 11, 2007 01:39PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.