#!/usr/bin/perl -w
use strict; #使用严格语法
use DBI;
sub set_GBK4Mysql{
#设定数据库字符集
my $sql = "SET CHARACTER SET GBK";
my $sth = $_ [0]->do($sql);
}
#检查mysql驱动是否安装
my $drh = DBI->install_driver( 'mysql' );
if ( !defined $drh ) {
die "不能装入mysql驱动: $!\n";
}
#连接mysql数据库
my $dbh = DBI->connect('DBI:mysql:dbname=cm3gw;host=127.0.0.1;port=13306',
'sioa_m3','sioa_m3') or die $DBI::errstr;
&set_GBK4Mysql($dbh);
my $query = "Select sp_port,service_name, memo "
." From sp_service_list"
." Where flag = 1 ";
#执行有结果集sql
my $sth = $dbh->prepare($query) or die "Can't prepare $query: $dbh->errstr\n";
my $result = $sth->execute or die "can't execute the query: $sth->errstr";
while(my @row = $sth->fetchrow_array) {
print "$row[0]\t$row[1]\t$row[2]\n";
}
# 指示没有更多的数据可以被取出,释放语句句柄,并且释放系统资源
$sth->finish;
#关闭连接
$dbh->disconnect;