我有一个数据库查询,我在eval中运行,以捕获错误.问题是错误消息正在输出到控制台,即使它被捕获.如何阻止错误消息执行此操作,因为我想自己解析它并吐回我自己的消息?
my $dbh = DBI->connect('dbi:Pg:dbname=database;host=localhost','user','pass',{RaiseError => 1} ); eval{ $sth = $dbh->prepare($sql); $sth->execute; }; if($@){ #Do my parse/print stuff here I know }
解决方法
@H_403_16@ 您可以指定’ PrintError =>您的连接呼叫中的0′(或使用 HandleError):my $dbh = DBI->connect('dbi:Pg:dbname=database;host=localhost',$user,$passwd,{ PrintError => 0,RaiseError => 1,});
或者设置每个语句句柄:
my $sth = $dbh->prepare("SELECT * from my_table"); $sth->{PrintError} = 0; $sth->execute(); ...etc.
my $result = eval { ... $sth->...etc. 1; } unless ($result) { # Do error handling..log/print $@ }