本文是参考了
1<db2 1="" 1.="" 10="" 2005.="" magazine="" number="" quarter="" volume="">杂志里的<perls db2="" free="" language="" of="" perl="" simple="" steps="" the="" these="" to="" use="" wisdom-follow="" with="">一文,可以说是一个摘抄(不同的是文章是中文)
2原文参见http://www.db2mag.com/story/showArticle.jhtml?articleID=59301551
3
41\. Perl的DB2驱动
5Perl语言本身就不多做介绍了。
61994年发布的DBI是Perl语言连接关系性数据库的标准。可以从dbi.perl.org获得DBI的源代码和文档。
7IBM在1995年发布了对于Perl的DB2驱动,这个驱动是符合DBI标准的,在Perl里这个驱动称为DBD::DB2 。可以从ibm.com/software/db2/perl获得DBD::DB2驱动的最新信息。
8
9注意:最近的DB2驱动需要至少Perl 5.005_03和DBI 1.21或以上的版本。
10
112\. 准备环境
12步骤如下
13(1)安装Perl语言环境
14Windows下可以从www.activestate.com获得Perl的安装包。
15安装后可以使用perl -v看看Perl的版本信息。
16DBI驱动和DBD::DB2驱动都是作为Perl的附加模块使用ppm工具安装的。安装方法参见(2), (3),安装时最好先去ibm.com/software/db2/perl上看看ppm后面的参数有没有变化。
17
18(2)安装DBI驱动
19ppm install http://ftp.esoftmatic.com/outgoing/DBI/5.8.4/DBI.ppd
20
21(3)安装DBD::DB2驱动
22ppm install http://ftp.esoftmatic.com/outgoing/DBI/5.8.4/DBD-DB2.ppd
23
24(4)安装DB2 runtime client
25
26
273\. 使用Perl连接DB2
28(1)使用DBI函数DBI->data_sources 来扫描DB2数据库目录并返回一个包含了有效数据源名称(DSN)的数组。
29\----------------datasources.pl-------------
30#!/usr/lib/perl -w
31#
32# This perl script prints the list of cataloged DB2 data-sources
33#
34use DBI;
35use DBD::DB2;
36use DBD::DB2::Constants;
37
38print "Operating Systems = $^O\n";
39print "Perl Binary = $^X\n";
40print "Perl Version = $]\n";
41print "DBI Version = $DBI::VERSION\n";
42print "DBD::DB2 Version = $DBD::DB2::VERSION\n\n";
43
44my @DB2DataSources = DBI->data_sources("DB2");
45
46print "Available DB2 DSNs:\n\n";
47
48foreach my $dsn ( @DB2DataSources )
49{
50print " $dsn \n";
51}
52\-------------------END----------------------
53
54(2)获取db2数据库的信息
55\----------datasourceInfo.pl--------------------------
56#!/usr/lib/perl -w
57#
58# This perl script prints the information DB2 database
59#
60use DBI;
61use DBD::DB2;
62use DBD::DB2::Constants;
63
64my $dsn = 'dbi:DB2:SAMPLE';
65my $uid = 'henry';
66my $pwd = 'happyday';
67
68my $dbh = DBI->connect( $dsn, $uid, $pwd ) || die "$DBI::errstr";
69
70print "Database Connection Information \n\n";
71printf( "Server Instance : %s\n", $dbh->get_info( SQL_SERVER_NAME ) );
72printf( "Database Server : %s\n", $dbh->get_info( SQL_DBMS_NAME ) );
73printf( "Database Version : %s\n", $dbh->get_info( SQL_DBMS_VER ) );
74printf( "Database Alias : %s\n", $dbh->get_info( SQL_DATA_SOURCE_NAME ) );
75printf( "Database Codepage : %s\n", $dbh->get_info( 2519 ) );
76printf( "Application Codepage: %s\n", $dbh->get_info( 2520 ) );
77printf( "Authoriztion Id : %s\n", $dbh->get_info( SQL_USER_NAME ) );
78printf( "Max Idntifier Len : %s\n", $dbh->get_info( SQL_MAX_IDENTIFIER_LEN ) );
79printf( "Max Table Name Len : %s\n", $dbh->get_info( SQL_MAX_TABLE_NAME_LEN ) );
80printf( "Max Index Size : %s\n", $dbh->get_info( SQL_MAX_INDEX_SIZE ) );
81printf( "Max Columns in Table: %s\n", $dbh->get_info( SQL_MAX_COLUMNS_IN_TABLE ) );
82\-------------------END----------------------
83
84(3)获取db2数据库的元数据(比如, 表结构)
85\----------tableinfo.pl--------------------------
86#!/usr/lib/perl -w
87#
88# This perl script prints the information of cataloged DB2 table
89#
90use DBI;
91use DBD::DB2;
92use DBD::DB2::Constants;
93
94$dsn = 'dbi:DB2:SAMPLE';
95$uid = 'henry';
96$pwd = 'happyday';
97
98# Connect to the SAMPLE database
99$dbh = DBI->connect( $dsn, $uid, $pwd ) || die "$DBI::errstr";
100
101# Get the tables for schema HENRY
102$sth = $dbh->table_info( { 'TABLE_CSHEM' => "HENRY" } );
103
104$table_counter = 0;
105while ( @row = $sth->fetchrow_array )
106{
107$catalog = $row[0];
108$schema = $row[1];
109$table = $row[2];
110
111$table_counter++;
112printf( "Table %d %s\n", $table_counter, $table );
113
114# Now get the column information for this table
115$sth_col = $dbh->column_info( $catalog, $schema, $table, '%' );
116if( $sth_col )
117{
118while( @row_col = $sth_col->fetchrow_array )
119{
120# @row_col has a lot more information. I'll just take
121# these three fields as an example
122$column_name = $row_col[3];
123$type_name = $row_col[5];
124$column_size = $row_col[6];
125
126printf( " %-24s%s(%s)\n", $column_name, $type_name, $column_size );
127}
128
129$sth_col->finish();
130}
131}
132
133$sth->finish();
134$dbh->disconnect;
135\-------------------END----------------------
136
137(4)执行SQL
138\----------executesql.pl--------------------------
139#!/usr/lib/perl -w
140#
141# This perl script manipulate DB2 table
142#
143use DBI;
144use DBD::DB2;
145use DBD::DB2::Constants;
146
147$dsn = 'dbi:DB2:SAMPLE';
148$uid = 'henry';
149$pwd = 'happyday';
150
151# Connect to the SAMPLE database
152$dbh = DBI->connect( $dsn, $uid, $pwd ) || die "$DBI::errstr";
153
154# Prepare our insert statement
155$sth = $dbh->prepare( "INSERT INTO sales VALUES('2005-06-25', 'Tom', 'Beijing', 15)");
156$sth->execute();
157$sth->finish();
158$dbh->disconnect;
159\-------------------END----------------------</perls></db2>