Perl:接收select count(*) from tables;返回值

数据库中内容如下图:

要查询 host = 'HOST:10.1.1.5'的记录数:

#!/usr/bin/perl
use DBI;
my $host = "localhost";
my $driver ="mysql";
my $database = "catchdata";
my $dsn = "DBI:$driver:database=$database:$host";
my $userid ="root";
my $password = "root123";
my $dbh = DBI->connect($dsn,$userid,$password) or die $DBI::errstr;
my $sth3;
$sth3 = $dbh->prepare("select count(*) from cmd_data  WHERE host ='HOST:10.1.1.5'");
my $a;
$sth3->execute() or die $DBI::errstr;
print "--------";

#使用 a 来接收返回的记录条数

$a = $sth3->fetchrow_array();
print "\$a is $a...\n";
$sth3->finish();

即可。

fetchrow_array():

fetchrow_array 作为一个字段数组取出下一行
fetchrow_arrayref 作为一个字段的引用数组取出下一行
fetchrow_hashref 作为一个哈希表的引用取出下一行

具体使用见此处:fetchrow_array与fetchrow_arrayref与fetchrow_hashref的用法

猜你喜欢

转载自blog.csdn.net/Winnycatty/article/details/89602615
今日推荐