设为首页 收藏本站
查看: 890|回复: 0

[经验分享] Windows下使用Perl连接DB2(转载)

[复制链接]

尚未签到

发表于 2015-12-26 17:40:08 | 显示全部楼层 |阅读模式
Windows下使用Perl连接DB2
呵呵,使用Perl语言连接DB2数据库,好玩吧   本文是摘抄杂志里的一文,原作者Michael Hoy & Grant Hutchison
原文参见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::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 install http://ftp.esoftmatic.com/outgoing/DBI/5.8.4/DBI.ppd

(3)安装DBD::DB2驱动
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::DB2;
use DBD::DB2::Constants;

print "Operating Systems = $^O\n";
print "Perl Binary = $^X\n";
print "Perl Version = $]\n";
print "DBI Version = $DBI::VERSION\n";
print "DBD::DB2 Version = $DBD::DB2::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::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 \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----------------------



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156749-1-1.html 上篇帖子: Perl语言入门-第九章-用正则表达式处理文本-习题 下篇帖子: perl多维哈希(实用篇)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表