`
zengshaotao
  • 浏览: 754047 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

perl连接数据库

    博客分类:
  • 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 ";
  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 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 ", $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 sales VALUES('2005-06-25', 'Tom', 'Beijing', 15)");
  $sth->execute();
  $sth->finish();
  $dbh->disconnect;
  -------------------END----------------------

  本文是摘抄杂志里的一文,原作者Michael Hoy & Grant Hutchison
  原文参见http://www.db2mag.com/story/showArticle.jhtml?articleID=59301551

分享到:
评论

相关推荐

    perl 连接数据库所需包

    perl dbi dbd unixodbc

    perl对数据库的操作

    简单的perl脚本。利用perl连接数据库,建立表并插入数据。

    Perl实现文件及数据库访问

    NULL 博文链接:https://dumaswong.iteye.com/blog/2017196

    Perl/C#连接Oracle/SQL Server

     Perl 连接数据库的思路都是:  1)使用DBI模块; 2)创建数据库连接句柄dbh; 3)利用dbh创建语句句柄sth; 4)利用sth执行sql语句; 5)利用sth处理数据。  如连接Oracle: my $dbh=DBI->connect(DBI:...

    perl的DBI安装包以及安装步骤

    该资源为Perl的安装包以及安装步骤,方便linux环境安装perl的DBI,解决在linux环境下使用perl连接数据库的问题。

    DBI-1.616.zip

    是perl连接数据库的接口。其是perl连接数据库的最优秀方法,他支持包括Orcal,Sybase,mysql,db2等绝大多数的数据库

    DBI-1.618.tar.gz

    perl连接数据库依赖模块包

    使用 Perl 访问 DB2 for Linux

    您将学习如何安装和使用 IBM DB2 Universal Database, Personal Developer\\\\\\\'s Edition 的 Perl 接口。您还将通过示例学习如何查询 DB2 Personal Developer\\\\\\\'s Edition 的样本数据库。

    通过dbi使用perl连接mysql数据库的方法

    通过使用DBI,用Perl可以很容易的连接到mysql数据库: 代码如下: 复制代码 代码如下:#!/bin/perl use DBI; # Connect to target DB my $dbh = DBI->connect(“DBI:mysql:database=eygle;host=localhost”,”...

    perldbi.pdf

    perl 连接数据库手册.Perl DBI是一个Perl语言的数据库访问API。DBI定义了一系列函数,变量和惯例提供一个独立于具体数据库的一致性的数据库接口。 。在应用程序和一个/多个数据库驱动器之间的一个瘦层。驱动器完成...

    Web应用安全:PerMySQL的安全连接方法.pptx

    Perl 5 中我们可以使用 DBI 模块来连接数据库。 DBI 英文全称:Database Independent Interface,中文称为数据库独立接口。 DBI 作为 Perl 语言中和数据库进行通讯的标准接口,它定义了一系列的方法,变量和常量,...

    在Ruby程序中连接数据库的详细教程

    Ruby DBI 模块为 Ruby 脚本提供了类似于 Perl DBI 模块的独立于数据库的接口。 DBI 即 Database independent interface,代表了 Ruby 独立于数据库的接口。DBI 在 Ruby 代码与底层数据库之间提供了一个抽象层,允许...

    Mastering perl/tk

    它被广泛地用于处理各种各样的任务,包括文件处理、系统管理、网络编程和数据库连接。早期的 Perl 用户不得不满足于使用命令行交互界面或者 Curse 全屏交互界面,抑或其它类似体系。但从 Tcl 语言中分离出来的 Tk ...

    PERL_DBI手册.pdf

    perl的DBI使用手册,详细描述了如何连接数据库,如何使用dbi模块,对于使用perl语言进行运维,开发的小伙伴希望带来帮助

    数据库NavicatforMySQL应用安装包+Java连接程序+数据库访问要点

    2010年以前Internet上流行的网站构架方式是LAMP,即是用Linux作为操作系统,Apache作为Web服务器,MySQL作为数据库,PHP(部分网站也使用Perl或Python)作为服务器端脚本解释器。由于这四个软件都是开放源码软件,...

    windows关于qt的数据库操作封装类以及用例,c++代码

    自动与sqlite3数据库做连接 数据的插入,查询,删除、更新操作 数据库建立与表建立 支持关于where语句查询的自动拼接。 支持text的模糊查询 windows 关于qt的数据库操作封装类 以及用例: SQLite,是一款轻型的...

    perl DBD-Oracle-1.22

    perl DBD-Oracle-1.22,连接oracle数据库驱动。

    Perl 实例精解(第三版).pdf

    第15章 Perl数据库编程 15.1 本章概述 15.2 Perl数据库编程 15.3 使用RDBMS的Perl编程 15.3.1 在Windows系统上安装Perl 15.3.2 使用PPM在Windows系统上安装Perl模块 15.3.3 安装RDBMS 15.3.4 为...

    PERL编程24学时教程.pdf

    第一部分 Perl基础 第1学时 Perl入门 3 1.1 安装Perl 3 1.1.1 等一等,也许你已经安装了Perl 4 1.1.2 在Windows 95/98/NT上安装Perl 5 1.1.3 在UNIX上安装Perl 6 1.1.4 在Macintosh系统上安装Perl 7 1.2 文档资料 7 ...

Global site tag (gtag.js) - Google Analytics