數據庫中內容如下圖:
要查詢 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的用法