DBI Expection question
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?