Windows下使用Perl连接DB2[转]
来源:互联网 发布:淘宝客api生成推广url 编辑:程序博客网 时间:2024/05/29 15:13
原文地址:
Perl语言本身就不多做介绍了。
1994年发布的DBI是Perl语言连接关系性数据库的标准。可以从dbi.perl.org获得DBI的源代码和文档。
IBM在1995年发布了对于Perl的DB2驱动,这个驱动是符合DBI标准的,在Perl里这个驱动称为DBD::DB2。可以从ibm.com/software/db2/perl获得DBD::DB2驱动的最新信息。
注意:最近的DB2驱动需要至少Perl 5.005_03和DBI 1.21或以上的版本。
2. 准备环境
步骤如下
(1)安装Perl语言环境
Windows下可以从www.activestate.com获得Perl的安装包。
安装后可以使用perl -v看看Perl的版本信息。
DBI驱动和DBD::DB2驱动都是作为Perl的附加模块使用ppm工具安装的。安装方法参见(2),(3),安装时最好先去ibm.com/software/db2/perl上看看ppm后面的参数有没有变化。
(2)安装DBI驱动
ppm installhttp://ftp.esoftmatic.com/outgoing/DBI/5.8.4/DBI.ppd
(3)安装DBD::DB2驱动
ppm installhttp://ftp.esoftmatic.com/outgoing/DBI/5.8.4/DBD-DB2.ppd
(4)安装DB2 runtime client
3. 使用Perl连接DB2
(1)使用DBI函数DBI->data_sources来扫描DB2数据库目录并返回一个包含了有效数据源名称(DSN)的数组。
----------------datasources.pl-------------
#!/usr/lib/perl -w
#
# This perl script prints the list of cataloged DB2data-sources
#
use DBI;
use DBD::DB2;
use DBD::DB2::Constants;
print "Operating Systems = $^O ";
print "Perl Binary = $^X ";
print "Perl Version = $] ";
print "DBI Version = $DBI::VERSION ";
print "DBD::DB2 Version = $DBD::DB2::VERSION ";
my @DB2DataSources = DBI->data_sources("DB2");
print "Available DB2 DSNs: ";
foreach my $dsn ( @DB2DataSources )
{
print " $dsn ";
}
-------------------END----------------------
(2)获取db2数据库的信息
----------datasourceInfo.pl--------------------------
#!/usr/lib/perl -w
#
# This perl script prints the information DB2 database
#
use DBI;
use DBD::DB2;
use DBD::DB2::Constants;
my $dsn = ''dbi:DB2:SAMPLE'';
my $uid = ''henry'';
my $pwd = ''happyday'';
my $dbh = DBI->connect( $dsn, $uid, $pwd ) || die"$DBI::errstr";
print "Database Connection Information ";
printf( "Server Instance : %s ", $dbh->get_info(SQL_SERVER_NAME ) );
printf( "Database Server : %s ", $dbh->get_info(SQL_DBMS_NAME ) );
printf( "Database Version : %s ", $dbh->get_info(SQL_DBMS_VER ) );
printf( "Database Alias : %s ", $dbh->get_info(SQL_DATA_SOURCE_NAME ) );
printf( "Database Codepage : %s ", $dbh->get_info(2519 ) );
printf( "Application Codepage: %s ",$dbh->get_info( 2520 ) );
printf( "Authoriztion Id : %s ", $dbh->get_info(SQL_USER_NAME ) );
printf( "Max Idntifier Len : %s ", $dbh->get_info(SQL_MAX_IDENTIFIER_LEN ) );
printf( "Max Table Name Len : %s ",$dbh->get_info( SQL_MAX_TABLE_NAME_LEN ));
printf( "Max Index Size : %s ", $dbh->get_info(SQL_MAX_INDEX_SIZE ) );
printf( "Max Columns in Table: %s ",$dbh->get_info( SQL_MAX_COLUMNS_IN_TABLE ));
-------------------END----------------------
(3)获取db2数据库的元数据(比如, 表结构)
----------tableinfo.pl--------------------------
#!/usr/lib/perl -w
#
# This perl script prints the information of cataloged DB2table
#
use DBI;
use DBD::DB2;
use DBD::DB2::Constants;
$dsn = ''dbi:DB2:SAMPLE'';
$uid = ''henry'';
$pwd = ''happyday'';
# Connect to the SAMPLE database
$dbh = DBI->connect( $dsn, $uid, $pwd ) || die"$DBI::errstr";
# Get the tables for schema HENRY
$sth = $dbh->table_info( { ''TABLE_CSHEM''=> "HENRY" } );
$table_counter = 0;
while ( @row = $sth->fetchrow_array )
{
$catalog = $row[0];
$schema = $row[1];
$table = $row[2];
$table_counter++;
printf( "Table %d %s ", $table_counter, $table );
# Now get the column information for this table
$sth_col = $dbh->column_info( $catalog, $schema,$table, ''%'' );
if( $sth_col )
{
while( @row_col = $sth_col->fetchrow_array)
{
# @row_col has a lot more information. I''ll just take
# these three fields as an example
$column_name = $row_col[3];
$type_name = $row_col[5];
$column_size = $row_col[6];
printf( " %-24s%s(%s) ", $column_name, $type_name, $column_size);
}
$sth_col->finish();
}
}
$sth->finish();
$dbh->disconnect;
-------------------END----------------------
(4)执行SQL
----------executesql.pl--------------------------
#!/usr/lib/perl -w
#
# This perl script manipulate DB2 table
#
use DBI;
use DBD::DB2;
use DBD::DB2::Constants;
$dsn = ''dbi:DB2:SAMPLE'';
$uid = ''henry'';
$pwd = ''happyday'';
# Connect to the SAMPLE database
$dbh = DBI->connect( $dsn, $uid, $pwd ) || die"$DBI::errstr";
# Prepare our insert statement
$sth = $dbh->prepare( "INSERT INTO salesVALUES(''2005-06-25'', ''Tom'', ''Beijing'', 15)");
$sth->execute();
$sth->finish();
$dbh->disconnect;
-------------------END----------------------
本文是摘抄杂志里的一文,原作者Michael Hoy & GrantHutchison
原文参见http://www.db2mag.com/story/showArticle.jhtml?articleID=59301551
- Windows下使用Perl连接DB2[转]
- windows下Perl安装使用
- windows下创建db2连接流程
- perl在windows下的安装使用
- DB2在windows下的安装和jdbc连接
- Windows平台上,使用eclipse连接到DB2数据库
- 在Linux下使用perl通过unixODBC连接SQLServer2000
- windows下使用perl Net:SMTP_auth发送邮件
- windows下perl脚本中安装使用parallel:ForkManager
- windows环境下Perl circos的安装与使用
- 【DB2】WAS使用JDBC连接DB2
- 命令窗口下连接db2
- 【转】windows上的db2连接linux上db2的通信问题
- windows下安装perl模块
- windows下安装perl Module
- windows下安装perl模块
- windows下编译vix-perl
- windows下Perl的HelloWorld
- ViewDefinitionSearch in Oracle[…
- Perl正则表达式模式测试程序
- 幽默
- Perl DBI 入门和Perl DBI连接MySQL…
- 通过 Perl 编程访问 DB2 Universal…
- Windows下使用Perl连接DB2[转]
- 利用Perl切片从列表数据中提取关键…
- perldoc命令的用法
- 微软面试题:设计程序让其CPU占用呈…
- 在Perl中获取当前目录的完整路径
- Perl:用File::Basename来获取文件…
- 离子炮的实现
- LINUX集群搭建必备:nat模式设置静态ip,达到上网与主机相互通信
- JQUERY实现可上下滚动的背景图片相册