Dbi Error Trapping
Contents |
login Username: * Password: * Create new accountRequest new password Home » DBI: handling database errors ( categories: databases ) Basically, there are two perl dbi error handling examples ways of handling database errors, check (almost) every DBI call for errors or set
Perl Dbi Catch Error
'RaiseError' attribute to '1ยด: -- Manual checking This way, you have to add code yourself to check for database error conditions, dbi connect error handling so after nearly every method call you should check if the operation completed successfully. There are two DBI methods that are very helpful to manually check for database errors: 'err' and 'errstr'. 'err' returns perl dbi execute error handling the native database engine error code from the last DBI method called. The code returned is usually an integer. 'errstr' returns the native database engine error message from the last DBI method called. Example: $dbh = DBI->connect($data_src, $user, $pwd) or die $DBI::errstr;
my $sth = $dbh->prepare("DELETE FROM table WHERE count < '?'");
$sth->execute(25);
if ( $sth->err )
{
die "ERROR! return
Dbi Raiseerror
code: . $sth->err . " error msg: " . $sth->errstr . "\n";
}
-- Setting 'RaiseError' attribute If DBI 'RaiseError' attribute is set to '1' (is '0' by default), then any database error will cause the DBI module to 'die' with an appropriate message. When using 'RaiseError', is recommended to set the 'PrintError' atribute to '0') Example: my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError => 1, PrintError => 0 });
Bookmark/Search this post with: | | | | » login or register to post comments You can also provide a Submitted by Kelicula on Wed, 04/15/2009 - 00:58. You can also provide a custom sub to handle errors with the RaiseError flag set. After establishing server connection: $dbh->{HandleError} = sub { my $error = shift; # do something with error...; }; Or in attributes: my $dbh = DBI->connect("DBI:......, { RaiseError => 1, HandleError => \&DBerror })|| die $DBI::errstr; sub DBerror { my $error = shift; # do something with error... } etc... Only disadvantage is knowing what line the error originated from in your script. -------- I'm unique just like everyone else! » login or register to post comments Home | Links | RSS feed | Forums Copyright ©
Check the returns from DBI callsRaiseErrorTest inserts/updates workedProtect calls you expect could legitimately fail with evalsUse transactions, protect them with eval and commit or roll them back after the evalConsider adding comments to SQLWhat you get in DBI out perl dbi escape of the box to help youDBIx::Log4perl Advantages of using DBIx::Log4perlWhere to get DBIx::Log4perlDBIx::Log4perl dependenciesDBIx::Log4perl perl dbi connect error handling documentationDBIx::Log4perl quick startExample DBIx::Log4perl outputCatching errors with DBIx::Log4perlAdding additional error logging to your Perl scriptLog4perl configuration fileChanging the default logging in
Perl Dbi Handleerror
DBIx::Log4perlLogging only SQL and parametersAppendix A: ResourcesIntroductionYou’ve read our other Perl tutorials, installed Perl DBI and a DBD and have started writing your application but it is not working properly. What can you do? This http://www.perlhowto.com/dbi_handling_database_errors tutorial ignores syntax errors in your Perl (which you can easily track down with perl -cw) and concentrates on locating those tough to find SQL, incorrect use of the DBI interface, transaction and logic errors.Good DBI programming practiseCheck the returns from DBI callsTest the return from DBI method calls return a success value. DBI->connect("dbi:ODBC:mydsn", "xxx", "yyy") or die "$DBI::errstr"; Normally this is a true and non-zero value, but in some cases http://www.easysoft.com/developer/languages/perl/dbi-debugging.html it is a true but zero value e.g. DBDs will return a true value for an insert operation that inserts zero rows, but if you know the insert should insert a row, you test the returned value is true and not "0E0" (which is true but zero). See the next section, "RaiseError" and "Test inserts worked".RaiseErrorThe handle attribute RaiseError is, by default, turned off. When the RaiseError attribute is turned on for a handle, it causes die to be called (or your HandleError routine) when an error occurs using DBI. If you turn RaiseError on in the DBI connect method, it will be inherited by all other handles created off that connection handle.Without RaiseError, the following code (by default) will print the error (because PrintError is on by default) but not cause your program to die: use DBI; $dbh = DBI->connect("dbi:ODBC:mydsn","xxx", "yyy"); $sth = $dbh->prepare("invalid sql"); $sth->execute; print "Got here without dying\n"; which when run, by default, prints an error saying your SQL is invalid but does not die, so your program continues making it difficult to track down where the error occurred or worse, compounding the problem, by continuing on to do other processing.In comparison, changing the DBI connect method to: $dbh = DBI->connect("dbi:ODBC:mydsn","xxx", "yyy", {RaiseError=>1}); causes your
RecentThreads NewestNodes Donate What'sNew on Jun 23, 2003 at 14:47UTC ( #268189=perlquestion: print w/replies, xml ) Need Help?? Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: Help ?!?!?Using DBI I call a PL/SQL package and hand it some variables. http://www.perlmonks.org/?node_id=268189 This package does some validation of the data, when the package is not happy with the data an SQL error is raised.What I want to do is instead of having DBI die, I want to see the error in a variable so I can process this and send back an error to the user.The variables are handed in via a webpage and errors should be returned on that page (thus perl dbi the dislike of a die)I have seen some solutions to this, but these usualy use a eval{} to solve the problem. Due to the application framework that I'm forced to work in this will not work as a die is always caught before my code can get to it.So what I wat is a way to find the errors returned without death.The ways I have atempted to get these errors dbi connect error from the DBI are: - my $error = $dbh->errstr; - my $error = $@The problem is that both are always undefined no matter how may errors the package returns. So my question is how do I find the errors without having the DBI calling dieThis is my config: - Perl v5.6.1 built for sun4-solaris - DBI DBI version 1.34 - Oracle DBD complited with Oracle 8.1.7 clientComment on DBI - PL/SQL error catchingSelect or Download Code Replies are listed 'Best First'. Re: DBI - PL/SQL error catching by gmax (Abbot) on Jun 23, 2003 at 15:15UTC If eval id not a viable solution, then you have to check every DBI call, but only after you instruct the DBI not to do anything in case of errors. #!/usr/bin/perl -w use strict; use DBI; # # ---- untested # my ($dns, $user, $password) = ("dbi:wathever:db","me","secret"); my $dbh = DBI->connect($dns,$user,$password, {RaiseError=>0 , PrintError=>0}) # <-- be quiet! or die "can't connect ($DBI::errstr)\n"; my $sth= $dbh->prepare("SELECT WRONG FROM NON_EXISTING"); if ($sth) { # fetch your records while (my $rec = $sth->fetchrow_arrayref()) { print "@$rec\n"; } } else { print "something was wrong: ", $dbh->errstr, "\n"; # deal with the problem here } [download] Notice that you can't use $dbh->errstr after a failed connectio