suppress stderr from perl DBI execute


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

我不想在控制台上显示$sth->execute的错误。如何抑制?

这由DBI句柄的PrintWarn和PrintError设置控制。我通常在建立初始数据库连接($dbh = DBI->connect($data_source, $user, $pass, { PrintWarn => 0, PrintError => 0 });)时全局关闭它们,因为我更喜欢自己进行错误处理和报告,但是也可以在每个语句级别设置它们,在单个execute中关闭它们,然后再打开它们,等等,通过使用set_err方法。

最新更新