使用:
MySQL 5.5 ActivePerl v5.14.1 Windows 7 64位
以下脚本在执行调用失败时有一个未定义的$ DBI :: errstr:
#!c:/perl/bin/perl.exe -w
use DBI;
my $dbh = DBI->connect('dbi:mysql:cq','root','password') or die "Connection Error: $DBI::errstr\n";
# This SQL should fail because of a foreign key constraint
my $sql = "delete from Player where ID=6462";
my $sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";
运行脚本时的输出是:
Use of uninitialized value $DBI::errstr in concatenation (.) or string at testmysql2.pl line 10.
SQL Error:
通过HeidiSQL运行的SQL提供了预期的外键错误: (我无法插入图像,因为我是新用户)
在Windows XP 32位上运行的相同脚本提供了预期的错误:
DBD::mysql::st execute failed: Cannot delete or update a parent row: a foreign key constraint fails (`cq`.`messageboard`, CONSTRAINT `messageboard_ibfk_1` FOREIGN KEY (`PlayerID`) REFERENCES `player` (`ID`)) at testmysql2.pl line 10.
SQL Error2: Cannot delete or update a parent row: a foreign key constraint fails (`cq`.`messageboard`, CONSTRAINT `messageboard_ibfk_1` FOREIGN KEY (`PlayerID`) REFERENCES `player` (`ID`))
注意:
无论SQL如何都会出现问题,例如SQL语法错误也会导致未定义的$ DBI :: errstr
有趣的是返回连接错误 - 例如,停止MySQL服务 - 运行脚本并按预期给出以下输出:
DBI connect('cq','root',...) failed: Can't connect to MySQL server on 'localhost' (10061) at testmysql2.pl line 4
Connection Error: Can't connect to MySQL server on 'localhost' (10061)
设置跟踪级别没有提供有用的信息(对我而言):
$sth->trace(5, "trace.txt");
$sth->execute or die "SQL Error: $DBI::errstr\n";
使用一段trace.txt并重新格式化为:
dbd_st_free_result_sets
mysql_st_internal_execute MYSQL_VERSION_ID 50147
parse_params statement delete from Player where ID=6462
dbd_st_execute returning imp_sth->row_num 18446744073709551614
execute= ( undef ) [1 items] at testmysql2.pl line 11
DESTROY for DBD::mysql::st (DBI::st=HASH(0x29dc0a8)~INNER) thr#2a97e8
正确的SQL按预期运行。我猜这是Windows 7 64位的错误? - 但是我不清楚/丢失了如何解决或找到正确的地方来报告/最终确定。
如果你走得这么远......感谢阅读!
答案 0 :(得分:1)
默认情况下,在PrintError启用时添加使用警告,这会导致DBI在出现故障时发出警告。另外,尝试将第二个$ DBI :: errstr更改为$ sth-> errstr。如果这不起作用,那么我怀疑执行是返回失败返回但DBD :: mysql没有在内部调用set_err。