Here's an example script that I loosely based on your script.

#!/usr/bin/perl use strict; use warnings; use DBI; my $dbh = DBI->connect("DBI:mysql:database=test;user=auser;password=ap +wd", {RaiseError => 1, PrintError => 1}); # set up error handling $dbh->{HandleError} = sub { handle_error(@_) }; my $sql = 'select wrong_col, correct_col from test limit 10'; our $handle_it; # This is only needed for this contrived example for $handle_it ( 0 .. 1 ) { print "\n", "-" x 40, "\n\n" if $handle_it; print "Trying HandleError (with RaiseError on):\n"; print "handle it? ", ($handle_it) ? 'yes' : 'no', "\n"; my $hashref = $dbh->selectall_hashref($sql, 'filename'); print "$_\t-->\t$hashref->{$_}\n" for (keys %{$hashref}); print "errstr says: ", $dbh->errstr, "\n" if $dbh->errstr; } sub handle_error { my ($error, $dbh) = @_; my $handled = 0; # did we handle the error? print "\n\tSub 'handle_error': $error\n"; if ( $handle_it ) { # if you want to say this error is not an err +or # clear the error $dbh->set_err(undef,undef); # set the new return value $_[2] = { wrong_col => 'bar', correct_col => 'foo' }; # update our handled flag $handled = 1; } else { # we acknowledge it was an error # muck around with $error here and store it in $_[0] # $_[0] is the error message seen by RaiseError/PrintError $_[0] = 'The gremlins are loose again!'; # potentially change the error too $dbh->set_err(1234, $_[0]); } print "\tEnd sub\n\n"; # return our handled flag return $handled; } __END__ Trying HandleError (with RaiseError on): handle it? no Sub 'handle_error': DBD::mysql::db selectall_hashref failed: T +able 'test .test' doesn't exist End sub The gremlins are loose again! at ./database.pl line 24. errstr says: Table 'test.test' doesn't exist [err was 1146 now 1234] The gremlins are loose again! ---------------------------------------- Trying HandleError (with RaiseError on): handle it? yes Sub 'handle_error': DBD::mysql::db selectall_hashref failed: T +able 'test .test' doesn't exist End sub correct_col --> foo wrong_col --> bar

In reply to Re: DBI::mysql error handling by Mr. Muskrat
in thread DBI::mysql error handling by svenXY

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.