mysql - 在 Windows-7-x64 上使用 DBI Perl 和 MySql 未定义 $DBI::errstr

标签 mysql perl dbi windows-7-x64

使用:

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 提供了预期的外键错误: (我是新用户,无法插入图片)

在 32 位 Windows XP 上运行的相同脚本提供了预期的错误:

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 位的错误? - 但我不清楚/迷失了如何解决或找到正确的地方来报告/明确地找到。

如果你走到这一步......感谢阅读!

最佳答案

添加使用警告,因为默认情况下 PrintError 处于启用状态,这会导致 DBI 在出现故障时发出警告。另外,尝试将第二个 $DBI::errstr 更改为 $sth->errstr。如果这不起作用,那么我怀疑 execute 正在返回失败返回,但 DBD::mysql 没有在内部调用 set_err。

关于mysql - 在 Windows-7-x64 上使用 DBI Perl 和 MySql 未定义 $DBI::errstr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7432944/

相关文章:

perl - 在Excel电子表格中打印文本

mysql - DBI begin_work 不适用于存储过程调用

php - 可以通过 PHP 从 Perl 执行 SQL 以提供全面的性能统计信息吗?

perl - DBIx::Class 插入有很多

perl - 如何在 Perl 中从 printf 呈现未定义的值?

sql - DBI:断开连接 - 问题

mysql - MariaDB 10.3 将字符串参数隐式转换为整数列失败

mysql - SQL:多次重复结果行,并对行编号

php - 在 Laravel 中给老师分配成绩(类(class))和科目(多对多)

MySQL 使用文件导入命令行创建数据库(如果不存在)