perl 测试脚本
作者:zlj2208
修改日期:2010年01月09日
转载请注明转自http://zlj2208.cublog.cn/
- 系统环境
系统版本:Fedora6 X86_64 DB2版本:DB2 v9.5.0.3 Perl版本: v5.8.8(系统自带) Perl-DBI版本:perl-DBI-1.52-1.fc6(系统自带) DBD-DB2版本:DBD-DB2-1.76
- 安装DBD-DB2
下载地址:http://search.cpan.org/~ibmtordb2/DBD-DB2-1.76/
导入下面环境变量: 根据自己的系统安装的路径,导入相应路径。 export LD_LIBRARY_PATH=/opt/ibm/db2/V9.5/lib64 export DB2_HOME=/opt/ibm/db2/V9.5
安装步骤: perl Makefile.PL make make test make install
模块安装位置: cat /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/DB2/.packlist /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/Bundle/DBD/DB2.pm /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/DB2.pm /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/DB2.pod /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/DB2/Constants.pm /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/DB2/Constants/Constants.bs /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/DB2/Constants/Constants.so /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/DB2/Constants/autosplit.ix /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/DB2/DB2.bs /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/DB2/DB2.so /usr/share/man/man3/Bundle::DBD::DB2.3pm /usr/share/man/man3/DBD::DB2.3pm
- 测试脚本.
1). perl连接远程DB2服务器: cat perl_connect_to_remote_db2.pl #!/usr/bin/perl -w $userid="db2inst1"; $password="passwd"; $db="sample"; $port=50000; #$hostname="localhost"; $hostname="xxx.xxx.xxx.xxx"; $protocol="tcpip"; $conn_string = "dbi:DB2:DATABASE=$db; HOSTNAME=$hostname; PORT=$port; PROTOCOL=$protocol; UID=$userid; PWD=$password;";
use DBI; $dbh = DBI->connect ("$conn_string",{RaiseError => 0}) or die "Can't connect to database $hostname:$db: $DBI::errstr"; my $stmt = "select * from employee"; my $sth = $dbh->prepare($stmt); $sth->execute(); my $col1; $sth->bind_col(1,$col1); while ($sth->fetch) { print "$col1\n"; } $sth->finish(); $dbh->disconnect();
2). perl连接本地DB2服务器: cat perl_connect_to_local_db2.pl #!/usr/bin/perl -w $userid="db2inst1"; $password="passwd"; $db="sample";
use DBI; $dbh = DBI->connect ("dbi:DB2:$db", "$userid", "$password") or die "Can't connect to $db database: $DBI::errstr"; my $stmt = "select * from employee"; my $sth = $dbh->prepare($stmt); $sth->execute(); my $col1; $sth->bind_col(1,$col1); while ($sth->fetch) { print "$col1\n"; } $sth->finish(); $dbh->disconnect() ;