[ic] DBI errors, and catching them in a [perl] block

Jeff Fearn jefffearn at gmail.com
Tue Nov 29 06:03:24 EST 2005

On 11/29/05, Jeff Boes <jeff at endpoint.com> wrote:
> What techniques do others use in catching DBI errors within a Perl
> block? For instance,
> [perl table=mytable]
> my $dbh = $Sql{mytable};
> $dbh->do(q{INSERT INTO mytable (a, b, c) VALUES (?, ?, ?)}, ...
> [/perl]
> I have not yet found any way to catch an INSERT error (e.g., bad data
> type, missing parameter, foreign key violation, unique constraint
> violation).

You use do when you don't care about what is returned, when you care
you must "do" more work...

---untested code---
my $dbh = $Sql{mytable};
my $sth = $dbh->prepare(q{INSERT INTO mytable (a, b, c) VALUES (?, ?, ?)});
$sth->execute() or die("ERROR!: " . $sth->errstr);

Hope that helps you in the right direction :)

Jeff Fearn

"Postmodernism: Once more without feeling." -- Geoffrey Nunberg

More information about the interchange-users mailing list