NOT getting "duplicate entry" error with DBI/MySQL
- From: "Todd Greene" <greenetoREMOVE@xxxxxxxxxxxxx>
- Date: Thu, 23 Jun 2005 16:52:19 GMT
Problem: When inserting a new row of data, I'm NOT getting a
"duplicate entry" error when I should be. Is this a bug in the DBI?
The DBD for MySQL? My Perl code?
Preliminary info:
Perl 5.8.4.810 (ActivePerl for MSWin32)
DBI 1.26
DBD-mysql 2.9005
MySQL 4.1.12-nt
Relevant table definition:
----------------------------------------------------------------
upc BIGINT UNSIGNED NOT NULL
sku VARCHAR(16) NOT NULL
model VARCHAR(20) NULL
name VARCHAR(50) NOT NULL
category1 INT UNSIGNED NOT NULL
category2 INT UNSIGNED NOT NULL
category3 INT UNSIGNED NOT NULL
description VARCHAR(255) NULL
PRIMARY KEY(upc)
UNIQUE INDEX(sku)
FOREIGN KEY(category1) REFERENCES rt_prodcategory1(id)
FOREIGN KEY(category2) REFERENCES rt_prodcategory2(id)
FOREIGN KEY(category3) REFERENCES rt_prodcategory3(id)
----------------------------------------------------------------
Relevant Perl code:
a. Data is being read in from a file.
b. AutoCommit is off (that's the "begin_work" command right before
the while loop).
----------------------------------------------------------------
my $sth = $dbh->prepare(qq{
INSERT $TABLE(
upc,
sku,
model,
name,
category1,
category2,
category3,
description)
VALUES(?,?,?,?,?,?,?,?)
});
if (!$sth)
{
print(STDERR "fatal error: prepare() failed!\n");
print(STDERR "(err msg: \"", $dbh->errstr, "\")\n");
return(16);
}
my $line;
my @field;
my $count_recs = 0;
$dbh->begin_work;
while ($line = <FILE_IN>)
{
++$count_recs;
chomp($line);
$line =~ s/\s+$//; # remove trailing whitespace
@field = split(/\t/,$line);
eval
{ # using SQL_VARCHAR for BIGINT (BIGINT type not in DBI)
$sth->bind_param(1,$field[0],SQL_VARCHAR);
$sth->bind_param(2,$field[1],SQL_VARCHAR);
$sth->bind_param(3,$field[2],SQL_VARCHAR);
$sth->bind_param(4,$field[3],SQL_VARCHAR);
$sth->bind_param(5,$field[4],SQL_INTEGER);
$sth->bind_param(6,$field[5],SQL_INTEGER);
$sth->bind_param(7,$field[6],SQL_INTEGER);
$sth->bind_param(8,$field[7],SQL_VARCHAR);
};
if ($@)
{
warn("database error: $DBI::errstr\n");
$sth->finish;
$dbh->rollback; # just die if rollback is failing
return(16);
}
$rv = $sth->execute;
if (!$rv)
{
print(STDERR "warning: execute failed! (",$sth->err,")\n");
print(STDERR " (msg: \"", $sth->errstr, "\")\n");
$sth->finish;
$dbh->rollback; # just die if rollback is failing
return(16);
}
}
$dbh->commit();
----------------------------------------------------------------
If I run my SQL code in MySQL Query Browser using the same data, the
database gives me the "duplicate entry" error (1062) just as it
should. But I'm not getting the error on my DBI "execute" like it
seems that I should be.
Can anyone tell me what I'm missing? Any insight appreciated.
? Todd Greene
.
- Prev by Date: RE: Oracle cursor help
- Next by Date: Re: Oracle cursor help - retry
- Previous by thread: Re: DBD::Oracle make test fails, undefined symbol: ociepgoe, oracle client 8.0.5
- Next by thread: Version 1.43 of DBD::Pg released
- Index(es):
Relevant Pages
- RE: Calling a PostgreSQL function via DBI
... If you try 'man DBI' or read one of the other available references, it will
make this point clear. ... However, using prepareand bind_paramis even more portable, if you can
get it to work for calling predefined functions. ... Calling a PostgreSQL function
via DBI ... (perl.dbi.users) - Possible bug in execute_array with dbd::mysql
... use DBI; ... my $dbh = DBI->connect( ... 'Duplicate entry
\'1\' for key 1', ... execute although the 3rd and 4th execute work - checked in the table
which ... (perl.dbi.users)