gpt4 book ai didi

perl - DBI-> connect上的错误处理

转载 作者:行者123 更新时间:2023-12-04 13:17:31 26 4
gpt4 key购买 nike

除了使用标准代码模具“无法连接:$ DBI::errstr\n”来处理错误之外,是否可以编写如下的自定义代码?

标准:

$dbstore = DBI->connect($dsn, $user, $pw,
{ora_session_mode => $mode, PrintError => 0, RaiseError => 0, AutoCommit => 0})
or die "Unable to connect: $DBI::errstr\n";

风俗:
$dbstore = DBI->connect($dsn, $user, $pw,
{ora_session_mode => $mode, PrintError => 0, RaiseError => 0, AutoCommit => 0});

if (!$dbstore)
{
CUSTOM_LOG_HANDLER("Could not connect to database: $DBI::errstr");
return;
}

样本标准代码:
#!/usr/bin/perl

# PERL MODULES WE WILL BE USING
use DBI;
use DBD::mysql;

# HTTP HEADER
print "Content-type: text/html \n\n";

# CONFIG VARIABLES
$platform = "mysql";
$database = "store";
$host = "localhost";
$port = "3306";
$tablename = "inventory";
$user = "username";
$pw = "password";

#DATA SOURCE NAME
$dsn = "dbi:mysql:$database:localhost:3306";

# PERL DBI CONNECT (RENAMED HANDLE)
$dbstore = DBI->connect($dsn, $user, $pw) or die "Unable to connect: $DBI::errstr\n";

谢谢您的时间。

最佳答案

您始终可以将自定义错误处理程序与DBI一起使用:

#!/usr/bin/perl

use strict;
use warnings;

use DBI;

sub handle_error {
my $message = shift;
#write error message wherever you want
print "the message is '$message'\n";
exit; #stop the program
}

my $dbh = DBI->connect(
"dbi:SQLite:foo",
"user",
"pass",
{
PrintError => 0,
HandleError => \&handle_error,
}
) or handle_error(DBI->errstr);

my $sth = $dbh->prepare("select * from doesntexist");

就是说,您应该记录错误,对于Web应用程序,Web服务器的日志很有意义。如果您担心Web日志中的噪音数量,则应集中精力纠正错误,而不要通过删除信息源来减少日志的噪音。

关于perl - DBI-> connect上的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6649456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com