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的用法

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章