Perl DBI : how to see failed query with bound values?

标签 perl dbi

这是来自 DBI 的标准插入示例手动的:

     my $query = q{
       INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
     };
     my $sth = $dbh->prepare($query) or die $dbh->errstr;
     while (<>) {
         chomp;
         my ($product_code, $qty, $price) = split /,/;
         $sth->execute($product_code, $qty, $price) or die ($query . " " . $dbh->errstr);
     }
     $dbh->commit or die $dbh->errstr;

我稍微修改了一下,所以我可以看到哪个查询失败(die ($query . " " . $dbh->errstr))。我仍然希望看到查询 带绑定(bind)值 (因为它被执行)。如何得到它?

编辑

顺便说一句,我也发现了一种尴尬的方式来查看带有绑定(bind)值的查询:您必须在查询中犯语法错误。例如,如果我像这样更改上面的查询:
     my $query = q{
       xINSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
     };

我得到了我想要的:

DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'xINSERT INTO sample (product_code, qty, price) VALUES ('1', '2', '3')' at line 1



有时它真的很有帮助。至少它对我有用。

最佳答案

您可以使用 DBI 的 ParamValues获取参数值,但您不太可能在 DBD 中找到任何方法来实际获取 SQL 中的参数,因为它们大多是在 SQL 解析后发送到数据库的。你可以看看DBIx::Log4perl查看在错误处理程序中如何使用 ParamValues。您可能还会发现 DBIx::Log4perl 的某些部分很有用。

关于Perl DBI : how to see failed query with bound values?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6462476/

相关文章:

mysql - Perl DBI 一次执行多个 SQL 查询

perl - 相同的查询,两种不同的方式,非常不同的性能

xml - 为什么 XML::LibXML 在使用命名空间时找不到此 xpath 查询的节点

regex - 删除一个字段中具有特定模式的行

linux - 替换特定参数分隔符后的字符串/行

mysql - 在 Perl 中终止 mysql 查询

mysql - Perl 数据库连接在 .pm 文件中不起作用

perl - 如何使用 perl 获取文件创建时间和当前时间的时间差(以小时为单位)

使用 foreach 创建 Perl CGI 表