查看: 3279|回复: 2

[Tips] Windows下使用Perl连接DB2

[复制链接]
论坛徽章:
75
授权会员
日期:2005-10-30 17:05:332012新春纪念徽章
日期:2012-02-13 15:08:092012新春纪念徽章
日期:2012-02-13 15:08:09ITPUB 11周年纪念徽章
日期:2012-10-09 18:06:20迷宫蛋
日期:2012-12-24 09:08:13茶鸡蛋
日期:2013-01-05 10:22:14咸鸭蛋
日期:2013-02-01 16:50:322013年新春福章
日期:2013-02-25 14:51:24紫蛋头
日期:2013-04-21 13:58:57本田
日期:2013-12-07 19:34:23
跳转到指定楼层
1#
发表于 2006-4-20 20:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Windows下使用Perl连接DB2

作者:zzjijun  


本文是参考了<DB2 Magazine Quarter 1. 2005. VOLUME 10 NUMBER 1>;杂志里的<Perls of Wisdom-Follow these simple steps to use the free Perl language with DB2>;一文,可以说是一个摘抄(不同的是文章是中文)
原文参见http://www.db2mag.com/story/showArticle.jhtml?articleID=59301551

1. Perl的DB2驱动
Perl语言本身就不多做介绍了。
1994年发布的DBI是Perl语言连接关系性数据库的标准。可以从dbi.perl.org获得DBI的源代码和文档。
IBM在1995年发布了对于Perl的DB2驱动,这个驱动是符合DBI标准的,在Perl里这个驱动称为DBD:B2 。可以从ibm.com/software/db2/perl获得DBD:B2驱动的最新信息。

注意:最近的DB2驱动需要至少Perl 5.005_03和DBI 1.21或以上的版本。

2. 准备环境
步骤如下
(1)安装Perl语言环境
Windows下可以从www.activestate.com获得Perl的安装包。
安装后可以使用perl -v看看Perl的版本信息。
DBI驱动和DBD:B2驱动都是作为Perl的附加模块使用ppm工具安装的。安装方法参见(2), (3),安装时最好先去ibm.com/software/db2/perl上看看ppm后面的参数有没有变化。

(2)安装DBI驱动
ppm install http://ftp.esoftmatic.com/outgoing/DBI/5.8.4/DBI.ppd

(3)安装DBD:B2驱动
ppm install http://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 DB2 data-sources
#
use DBI;
use DBD:B2;
use DBD:B2::Constants;

print "Operating Systems = $^O\n";
print "Perl Binary = $^X\n";
print "Perl Version = $]\n";
print "DBI Version = $DBI::VERSION\n";
print "DBD:B2 Version = $DBD:B2::VERSION\n\n";

my @DB2DataSources = DBI->;data_sources("DB2";

print "Available DB2 DSNs:\n\n";

foreach my $dsn ( @DB2DataSources )
{
print " $dsn \n";
}
-------------------END----------------------

(2)获取db2数据库的信息
----------datasourceInfo.pl--------------------------
#!/usr/lib/perl -w
#
# This perl script prints the information DB2 database
#
use DBI;
use DBD:B2;
use DBD:B2::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 \n\n";
printf( "Server Instance     : %s\n", $dbh->;get_info( SQL_SERVER_NAME ) );
printf( "Database Server     : %s\n", $dbh->;get_info( SQL_DBMS_NAME ) );
printf( "Database Version    : %s\n", $dbh->;get_info( SQL_DBMS_VER ) );   
printf( "Database Alias      : %s\n", $dbh->;get_info( SQL_DATA_SOURCE_NAME ) );
printf( "Database Codepage   : %s\n", $dbh->;get_info( 2519 ) );
printf( "Application Codepage: %s\n", $dbh->;get_info( 2520 ) );
printf( "Authoriztion Id     : %s\n", $dbh->;get_info( SQL_USER_NAME ) );
printf( "Max Idntifier Len   : %s\n", $dbh->;get_info( SQL_MAX_IDENTIFIER_LEN ) );
printf( "Max Table Name Len  : %s\n", $dbh->;get_info( SQL_MAX_TABLE_NAME_LEN ) );
printf( "Max Index Size      : %s\n", $dbh->;get_info( SQL_MAX_INDEX_SIZE ) );
printf( "Max Columns in Table: %s\n", $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 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";

# 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\n", $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)\n", $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 sales VALUES('2005-06-25', 'Tom', 'Beijing', 15)";
$sth->;execute();
$sth->;finish();
$dbh->;disconnect;
-------------------END----------------------
论坛徽章:
2
设计板块每日发贴之星
日期:2008-02-21 01:05:48奥运会纪念徽章:击剑
日期:2008-05-05 16:01:05
2#
发表于 2008-3-11 10:21 | 只看该作者
Nice job !! thx....

使用道具 举报

回复
招聘 : Linux运维
论坛徽章:
235
紫蜘蛛
日期:2007-09-26 17:05:46玉兔
日期:2007-09-26 17:05:05现任管理团队成员
日期:2011-05-07 01:45:08玉兔
日期:2006-08-29 20:38:48紫蜘蛛
日期:2007-09-26 17:05:34阿斯顿马丁
日期:2013-11-19 10:38:16奔驰
日期:2013-10-16 09:08:58红旗
日期:2014-01-09 11:57:39路虎
日期:2013-08-13 14:52:35林肯
日期:2015-05-19 13:01:16
3#
发表于 2008-3-11 10:48 | 只看该作者
,但是哪个表情能不能去掉??

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表