MySQL Forums
Forum List  »  Perl

DBI Expection question
Posted by: Jim Babcock
Date: June 09, 2005 10:43AM

I'm writing a perl script that inserts records into a table that uses 2 columns as the primary key. When I violate the unique constraint, how do I recover and continue processing?

$dbh = DBI->connect('dbi:mysql:reports', 'root','rep0rts') or Die ("test","WSB","Could not connect to database" . $dbh->errstr);

my $qstr = qq{select count from wsb_billing_urls where url = ? and tstamp = ?};
my $q_stmt = $dbh->prepare($qstr);
my $ustr = qq{update wsb_billing_urls set count = ? where url = ? and tstamp = ?};
my $u_stmt = $dbh->prepare($ustr);
my $istr = qq{insert wsb_billing_urls (url,tstamp,count) values (?,?,?)};
my $i_stmt = $dbh->prepare($istr);
my $errstr = qq{insert err_urls (url,tstamp,count) values (?,?,?)};
my $err_stmt = $dbh->prepare($errstr);

[snip]

#insert
$i_stmt->execute($url,$dateStr,$urls{$key}) or print "could not load: $url,$dateStr,$urls{$key}\n";

This prints the error message, but still exits.

I'd really like to slam the offending record into an "error" table and tried something like:

$i_stmt->execute($url,$dateStr,$urls{$key}) or $err_stmt->execute($url,$dateStr,$urls{$key})

but that fails to compile because it claims that err_stmt is perhaps undeclared.

How do I make my script not exit when I hit a table constraint error?

Options: ReplyQuote


Subject
Written By
Posted
DBI Expection question
June 09, 2005 10:43AM


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.