有时候需要perl操作mysql数据库,可以通过DBI实现,需要的朋友可以参考下
DBI安装:DBI详细信息参考:
#! /usr/bin/perl
use DBI;
my $driver="DBI:mysql";
my $database="perl_test";
my $user="root";
my $host="localhost";
my $passwd="root";
my $rules="alert_rules";
my $dbh = DBI->connect("$driver:database=$database;host=$host;user=$user;password=$passwd")
or die "Can't connect: " . DBI->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
my $sth=$dbh->prepare("select app_name,receivers from $rules ");
$sth->execute() or die "Can't prepare sql statement". $sth->errstr;
# 打印获取的数据
while(@recs=$sth->fetchrow_array){
print $recs[0].":".$recs[1]."\n";
}
$sth->finish();
$dbh->disconnect();