perl 操作mysql 数据库

#! /usr/bin/perl 


use DBI; 
my $driver="DBI:mysql";
my $host="10.21.1.62";
my $port=3306; 
my $database="mario"; 
my $user="root";  
my $passwd="Paic1234"; 
my $rules="mo_alert_set"; 


my $dbh = DBI->connect("$driver:database=$database;host=$host;port=$port;user=$user;password=$passwd") or die "Can't connect: " . DBI->errstr; 


my $sth=$dbh->prepare("select * from $rules"); 


$sth->execute() or die "Can't prepare sql statement". $sth->errstr; 




# 打印获取的数据 
while(@recs=$sth->fetchrow_array){ 
my $length = @recs;


foreach my $i ( 0 .. $length) {
print @recs[$i].",";
}
print  "\n";



$sth->finish(); 
$dbh->disconnect(); 

你可能感兴趣的:(perl 操作mysql 数据库)