eval 捕获dbi错误

[root@dr-mysql01 ~]# cat t2.pl 
use DBI;
my $dbUser='zabbix';
my $user="root";
my $passwd="1234567";
my $TABLE_NAME='history_uint';
my $date='201605';
my $dbh  = DBI->connect("dbi:mysql:database=zabbix;host=192.168.32.55;port=3306",$user,$passwd,{RaiseError => 1}) or die "can't connect to database ". DBI-errstr;
my $hostSql = qq{select TABLE_NAME,PARTITION_NAME from INFORMATION_SCHEMA.PARTITIONS where TABLE_NAME='$TABLE_NAME' and PARTITION_NAME like "p$date%";};
print "\$hostSql is $hostSql\n";
my ($a1, $a2);
#if ($a1){
my $selStmt = $dbh->prepare($hostSql);
$selStmt->execute();
$selStmt->bind_columns(undef, \$a1,\$a2);
print "\$a1 is $a1\n";
print "\$a2 is $a2\n";
print $selStmt->fetch();
print "\n";
 eval {$selStmt->fetch()};
print "\$@ is $@\n";;
print "\n";
[root@dr-mysql01 ~]# perl t2.pl 
$hostSql is select TABLE_NAME,PARTITION_NAME from INFORMATION_SCHEMA.PARTITIONS where TABLE_NAME='history_uint' and PARTITION_NAME like "p201605%";
$a1 is 
$a2 is 
ARRAY(0x2324778)
$@ is 



[root@dr-mysql01 ~]# cat t2.pl 
use DBI;
my $dbUser='zabbix';
my $user="root";
my $passwd="1234567";
my $TABLE_NAME='history_uint';
my $date='201604';
my $dbh  = DBI->connect("dbi:mysql:database=zabbix;host=192.168.32.55;port=3306",$user,$passwd,{RaiseError => 1}) or die "can't connect to database ". DBI-errstr;
my $hostSql = qq{select TABLE_NAME,PARTITION_NAME from INFORMATION_SCHEMA.PARTITIONS where TABLE_NAME='$TABLE_NAME' and PARTITION_NAME like "p$date%";};
print "\$hostSql is $hostSql\n";
my ($a1, $a2);
#if ($a1){
my $selStmt = $dbh->prepare($hostSql);
$selStmt->execute();
$selStmt->bind_columns(undef, \$a1,\$a2);
print "\$a1 is $a1\n";
print "\$a2 is $a2\n";
print $selStmt->fetch();
print "\n";
 eval {$selStmt->fetch()};
print "\$@ is $@\n";;
print "\n";
[root@dr-mysql01 ~]# perl t2.pl 
$hostSql is select TABLE_NAME,PARTITION_NAME from INFORMATION_SCHEMA.PARTITIONS where TABLE_NAME='history_uint' and PARTITION_NAME like "p201604%";
$a1 is 
$a2 is 

DBD::mysql::st fetch failed: fetch() without execute() at t2.pl line 19.
$@ is DBD::mysql::st fetch failed: fetch() without execute() at t2.pl line 19.

阅读更多
个人分类: Perl DBI
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭