Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: simple question about printing sql 2000 error message

by gmax (Abbot)
on Oct 07, 2003 at 06:59 UTC ( #297216=note: print w/replies, xml ) Need Help??


in reply to simple question about printing sql 2000 error message

The best way of catching errors in a DBI operation is through eval.

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 }
_ _ _ _ (_|| | |(_|>< _|

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://297216]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-12-07 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?