问题
Been dealing with this since 2011. Usually able to work around it. Now a situation has arisen where I really need to be able to handle errors, and I'm thinking, people have known about this problem for years, it seems like the sort of thing high up on the fix list, but the latest DBI upgrade and the problem still has not been resolved. Has anyone found a work-around, or know how to hack the libraries and get DBI::errstr to carry a value when there is an error?
Here's my code (And yes I am aware of the problems w/ a blank root password):
use strict;
use DBI;
use DBD::mysql;
use Data::Dumper;
my $platform = "mysql";
my $database = "test";
my $host = "localhost";
my $port = "3306";
my $user = "root";
my $pw = "";
my $dsn;
$dsn = "dbi:mysql:$database:$host:$port";
my $DBI_connect = DBI->connect($dsn, $user, $pw, {RaiseError => 1,PrintError=>1}) or die "$DBI::errstr\n";
my $sql_str = 'INSERT INTO test_tablexx (\'xxx) VALUES (blah)';
my $DBI_query_handle = $DBI_connect->prepare( $sql_str ) or die "Can't prepare SQL statement: $DBI::errstr\n";
my $return = $DBI_query_handle->execute() or warn "$DBI::errstr\n";
print "sys arry".Dumper $@."\n";
print "err: ".$DBI_query_handle->err."\n";
print "errstr: ".$DBI_query_handle->errstr."\n";
print "state: ".$DBI_query_handle->state."\n";
print "err: ".$DBI_connect->err."\n";
print "errstr: ".$DBI_connect->errstr."\n";
print "state: ".$DBI_connect->state."\n";
print Dumper $DBI_query_handle;
print "\n\n-------------------------------------------------------------------\n\n";
my $hash = DBI->installed_versions;
print Dumper $hash;
Now, the table is invalid, the column has a quote in the middle of it and the value is an unquoted string. The should light up the error handling like a christmas tree.
It does pick up an error, if I change the execute statement to die "$DBI::errstr\n"; it will die, but with no message at all.
Yet here is the result:
sys arry$VAR1 = '
';
err:
errstr:
state:
err:
errstr:
state:
$VAR1 = bless( {}, 'DBI::st' );
-------------------------------------------------------------------
$VAR1 = {
'DBD::SQLite' => '1.37',
'DBD::ExampleP' => '12.014311',
'DBD::Sponge' => '12.010003',
'DBD::Pg' => bless( {
'original' => '2.19.3',
'qv' => 1,
'version' => [
2,
19,
3
]
}, 'version' ),
'DBD::Gofer' => '0.015327',
'DBD::DBM' => '0.08',
'DBD::mysql' => '4.022',
'DBD::ADO' => '2.99',
'DBD::ODBC' => '1.41',
'DBI' => '1.631',
'DBD::File' => '0.42'
};
The last bit is a dump of the version information, so y'all can see I am up to date with my DBI install.
Now, when I change the database to something that does not exist, for example: my $database = "testxxx", it does indeed fire an error:
DBI connect('testxx:localhost:3306','root',...) failed: Unknown database 'testxx' at ../sql_tester.pl line 17.
A final bit of information for you, when I dump $DBI::lasth, I can see that the error methods are undef:
print Dumper $DBI::lasth;
$VAR1 = bless( {
'FetchHashKeyName' => 'NAME',
'TraceLevel' => 0,
'ImplementorClass' => 'DBD::mysql::db',
'dbi_imp_data' => undef,
'State' => \undef,
'Username' => 'root',
'Errstr' => \undef,
'ChildHandles' => [
bless( {}, 'DBI::st' )
],
'Driver' => bless( {
'Attribution' => 'DBD::mysql by Patrick Galbraith',
'FetchHashKeyName' => 'NAME',
'TraceLevel' => 0,
'ImplementorClass' => 'DBD::mysql::dr',
'State' => \undef,
'Version' => '4.022',
'Errstr' => \undef,
'ChildHandles' => [
bless( {}, 'DBI::db' )
],
'Name' => 'mysql',
'Err' => \undef
}, 'DBI::dr' ),
'Statement' => 'INSERT INTO test_tablexx (\'xxx) VALUES (blah)',
'Name' => 'test:localhost:3306',
'dbi_connect_closure' => sub { "DUMMY" },
'Err' => \undef
}, 'DBI::db' );
So please please good people of SO, what do I do when one of the most important principles of programming - error handling - is not working in the centerpiece module of my program?!
回答1:
According to the DBD::mysql changelog, this was fixed in v4.024 (released September 17, 2013). Your output shows that you're only using v4.022, so you'll need to upgrade DBD::mysql. You can find the bug report detailing the issue here.
来源:https://stackoverflow.com/questions/21357395/win-64-strawberry-perl-dbi-no-error-reporting-bug-still-not-fixed