Perl with db2

RedHat 6.3 64-bit Linux
[root@dbserv ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.3 (Santiago)

DB2 10.1
$ db2level
DB21085I  This instance or install (instance name, where applicable:
"db2inst1") uses "64" bits and DB2 code release "SQL10012" with level
identifier "0203010E".
Informational tokens are "DB2 v10.1.0.2", "s121127", "IP23394", and Fix Pack
"2".
Product is installed at "/opt/ibm/db2/V10.1".


Perl v5.10.1 (as part of RedHat 6.3)
$ perl --version

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

Copyright 1987-2009, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

$

访问页面http://search.cpan.org/~ibmtordb2/,下载驱动文件DBD-DB2-1.85.tar.gz

解压缩文件
[root@dbserv db2inst1]# tar -xvf DBD-DB2-1.85.tar.gz
[root@dbserv DBD-DB2-1.85]# pwd
/home/db2inst1/DBD-DB2-1.85
[root@dbserv DBD-DB2-1.85]#


[root@dbserv DBD-DB2-1.85]# export DB2_HOME=/opt/ibm/db2/V10.1
[root@dbserv DBD-DB2-1.85]# export DB2LIB=/opt/ibm/db2/V10.1/lib64
[root@dbserv DBD-DB2-1.85]# perl Makefile.PL
Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at Makefile.PL line 8.
BEGIN failed--compilation aborted at Makefile.PL line 8.
[root@dbserv DBD-DB2-1.85]# yum install perl-DBI
[root@dbserv DBD-DB2-1.85]# yum install perl-devel
[root@dbserv DBD-DB2-1.85]#
[root@dbserv DBD-DB2-1.85]# perl Makefile.PL

Configuring DBD::DB2...
Remember to actually read the README and CAVEATS files!

Using DB2 in "/opt/ibm/db2/V10.1"
System: perl5.010001 DBI1.609 linux c6b8.bsys.dev.centos.org 2.6.32-220.el6.x86_64 #1 smp tue dec 6 19:48:22 gmt 2011 x86_64 x86_64 x86_64 gnulinux  x86_64-linux-thread-multi dl_dlopen.xs
Compiler: gcc -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
Includes:  -I"/opt/ibm/db2/V10.1/include" -I"/usr/local/lib64/perl5/auto/DBI" -I"/usr/lib64/perl5/auto/DBI" -I"/usr/lib64/perl5/vendor_perl/auto/DBI" -I"/usr/local/lib64/perl5/auto/DBI"
Libraries: -L/opt/ibm/db2/V10.1/lib64 -ldb2

Checking if your kit is complete...
Looks good
Checking if your kit is complete...
Looks good
Writing Makefile for DBD::DB2::Constants
Writing Makefile for DBD::DB2
[root@dbserv DBD-DB2-1.85]# make
[root@dbserv DBD-DB2-1.85]# make install

用root安装完成以后,下面用db2实例用户运行一个测试程序
$ echo $LD_LIBRARY_PATH
/home/db2inst1/sqllib/lib64:/home/db2inst1/sqllib/lib32
$

做一个很简单的helloworld.pl程序

#!/usr/bin/perl
use DBI;

my $database='dbi:DB2:SAMPLE';
my $user='db2inst1';
my $password='hello';

my $dbh = DBI->connect($database, $user, $password)
or die "Can't connect to $database: $DBI::errstr";

my $sth = $dbh->prepare(
q{ SELECT 'Hello World' FROM SYSIBM.SYSDUMMY1 })
or die "Can't prepare statement: $DBI::errstr";

my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";

$col1 = $sth->fetchrow();
print "$col1\n";

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

测试整个程序
$ ./helloworld.pl
Hello World
$




#!/usr/bin/perl
use DBI;
# Text::ASCIITable provides an easy way to create elegant table reports
# available in RepoForge repository
# install with: "sudo yum install perl-Text-ASCIITable"
# download with: wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el6/en/i386/rpmforge/RPMS/perl-Text-ASCIITable-0.18-1.el6.rf.noarch.rpm
use Text::ASCIITable;

my $database='dbi:DB2:SAMPLE';
my $user='db2inst1';
my $password='hello';

my $dbh = DBI->connect($database, $user, $password)
or die "Can't connect to $database: $DBI::errstr";

# select all collumns from db2 sample table DEPT
my $sth = $dbh->prepare(
q{ SELECT DEPTNO, DEPTNAME, MGRNO, ADMRDEPT, LOCATION
FROM DB2INST1.DEPT })
or die "Can't prepare statement: $DBI::errstr";

my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";

# prepare Ascii Table
$t = Text::ASCIITable->new({ headingText => 'Perl+DB2 Hello World: Sample DEPT table' });
$t->setCols('Dept #','Dept Name','Mgr #','Dept Admin','Location');

# fetch loop
while (($deptn, $deptnam, $mgrno, $admrdept, $location) = $sth->fetchrow()) {
$t->addRow($deptn, $deptnam, $mgrno, $admrdept, $location);
}

# close and print Ascii Table
$t->addRowLine();
print $t;

# check for problems which may have terminated the fetch early
warn $DBI::errstr if $DBI::err;

# bye
$sth->finish;
$dbh->disconnect;

$ ./helloworld.pl
.------------------------------------------------------------------------.
|                 Perl+DB2 Hello World: Sample DEPT table                |
+--------+------------------------------+--------+------------+----------+
| Dept # | Dept Name                    | Mgr #  | Dept Admin | Location |
+--------+------------------------------+--------+------------+----------+
| A00    | SPIFFY COMPUTER SERVICE DIV. | 000010 | A00        |          |
| B01    | PLANNING                     | 000020 | A00        |          |
| C01    | INFORMATION CENTER           | 000030 | A00        |          |
| D01    | DEVELOPMENT CENTER           |        | A00        |          |
| D11    | MANUFACTURING SYSTEMS        | 000060 | D01        |          |
| D21    | ADMINISTRATION SYSTEMS       | 000070 | D01        |          |
| E01    | SUPPORT SERVICES             | 000050 | A00        |          |
| E11    | OPERATIONS                   | 000090 | E01        |          |
| E21    | SOFTWARE SUPPORT             | 000100 | E01        |          |
| F22    | BRANCH OFFICE F2             |        | E01        |          |
| G22    | BRANCH OFFICE G2             |        | E01        |          |
| H22    | BRANCH OFFICE H2             |        | E01        |          |
| I22    | BRANCH OFFICE I2             |        | E01        |          |
| J22    | BRANCH OFFICE J2             |        | E01        |          |
'--------+------------------------------+--------+------------+----------'
$



请使用浏览器的分享功能分享到微信等