这是来自 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/