my $dbh = DBI->connect("dbi:driver:database",
"user","password",
{RaiseError=>1, PrintError=>0})
or die "can't connect ($DBI::errstr)\n";
my $sth;
eval {
$sth = $dbh->prepare( $sql );
$sth->execute;
};
if ($@) {
print "There was an error ($DBI::errstr)\n";
# do something appropriate
}
Notice that RaiseError must be on, to be sure it will raise an exception in case of error. The DBI docs have all the details of this procedure.
I believe you would benefit from a walk through our Tutorials about database programming. Start with this one.
As a side note, instead of using
"INSERT INTO t1 (lsname) VALUES ('$lsname')"; consider the placeholder mechanism, which will make sure that your field is properly quoted.
my $sql = "INSERT INTO t1 (lsname) VALUES ( ? )";
my $sth;
eval {
$sth = $dbh->prepare( $sql );
$sth->execute ($lsname);
};
if ($@) {
print "There was an error ($DBI::errstr)\n";
# do something appropriate
}
|