perl 登录oracle,Perl 联接 Oracle 示例

Perl 连接 Oracle 示例

1.首先需要先下载3个模块

地址:

ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/by-module/DBI/

DBI-1.622.tar.gz

DBD-Oracle-1.50.tar.gz

Data-ShowTable-3.3.tar.gz (data目录中)

2. 安装

[root@james-test oracle]# tar xzvf DBI-1.622.tar.gz

[root@james-test oracle]# tar xzvf DBD-Oracle-1.50.tar.gz

[root@james-test oracle]# tar xzvf Data-ShowTable-3.3.tar.gz

以下安装命令都使用的root用户

安装dbi

cd DBI-1.622

perl Makefile.PL

make

make install

安装showtable

cd Data-ShowTable-3.3

perl Makefile.PL

make

make install

安装dbd(需要先设置oracle_home)

cd DBD-Oracle-1.50

export ORACLE_HOME=/u01/apps/oracle/product/11gr2/db_1

perl Makefile.PL

make

make install

模块安装完毕

3.下面来两个示例

example 1  简单连接数据库示例:

2.p :

#!/usr/bin/perl

#perl script used to connect to Oracle

use strict;

use DBI;

my $tnsname="david";

my $username="dexter";

my $password="xiaojun";

my $dbh=DBI->connect("dbi:Oracle:$tnsname", $username, $password) or die "Cannot conenct db: $DBI::errstr\n";

print "I have connected to the Oracle database!\n";

$dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";

print "Disconnected from Oracle databae!\n";

连接成功

[oracle@james-test ~]$ ./2.p

I have connected to the Oracle database!

Disconnected from Oracle databae!

example 2  连接数据库并且查询表示例:

稍微修改一下

2.p:

#!/usr/bin/perl

#perl script used to connect to Oracle

use strict;

use DBI;

my @row ;

my $tnsname="david";

my $username="dexter";

my $password="xiaojun";

my $dbh=DBI->connect("dbi:Oracle:$tnsname", $username, $password) or die "Cannot conenct db: $DBI::errstr\n";

print "I have connected to the Oracle database!\n";

my $sth=$dbh->prepare(" select * from emp_t " );

$sth->execute();

while (@row = $sth->fetchrow_array()) {

print "row's @row\n"

}

$dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";

print "Disconnected from Oracle databae!\n";

[oracle@james-test ~]$ ./2.p

I have connected to the Oracle database!

row's 7369 SMITH CLERK 7902 17-DEC-80 800  20

row's 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

row's 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

row's 7566 JONES MANAGER 7839 02-APR-81 2975  20

row's 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

row's 7698 BLAKE MANAGER 7839 01-MAY-81 2850  30

row's 7782 CLARK MANAGER 7839 09-JUN-81 2450  10

row's 7788 SCOTT ANALYST 7566 19-APR-87 3000  20

row's 7839 KING PRESIDENT  17-NOV-81 5000  10

row's 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

row's 7876 ADAMS CLERK 7788 23-MAY-87 1100  20

row's 7900 JAMES CLERK 7698 03-DEC-81 950  30

row's 7902 FORD ANALYST 7566 03-DEC-81 3000  20

row's 7934 MILLER CLERK 7782 23-JAN-82 1300  10

Disconnected from Oracle databae!

大功告成

文中涉及的三个包我已经上传,详细看下面的地址:

http://download.csdn.net/detail/renfengjun/4563255

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Perl.for.Oracle.DBAs 绝对经典! Our look is the result of reader comments, our own experimentation, and feedback from distribution channels. Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects. The animals on the cover of Perl for Oracle DBAs are thread-winged lacewings. Lacewings can be found all over the world, primarily in warmer climates. They live mostly in sheltered, sandy areas such as wooded dunes, forest floors, and riverbanks, until they reach adult form, at which time their wings enable them to roam more freely. In their larvae state, lacewings prey voraciously on such unsuspecting victims as aphids, mites, and scale insects. They hide under pieces of wood or debris, wait for insects to pass, then attack with their pincer-like mandibles. Lacewings undergo full metamorphosis throughout their lives. The adult form is characterized by two sets of wings, a long, slender abdomen, and clubbed antennae. Lacewings are one type of many nerve-winged insects because of the intricate pattern of lines (nerves) running through their transparent wings. Darren Kelly was the production editor for Perl for Oracle DBAs. Nancy Crumpton provided production services and wrote the index. Jan Fehler was the copyeditor. Tatiana Apandi Diaz and Claire Cloutier provided quality control. Emma Colby designed the cover of this book, based on a series design by Edie Freedman. The cover image is a 19th-century engraving from The Riverside Natural History: Volume 2. Emma Colby produced the cover layout with QuarkXPress 4.1 using Adobe's ITC Garamond font. David Futato designed the interior layout. This book was converted to FrameMaker 5.5.6 with a format conversion tool created by Erik Ray, Jason McIntosh, Neil Walls, and Mike Sierra that uses Perl and XML technologies. The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont's TheSans Mono Condensed. The illustrations that appear in the book were produced by Robert Romano and Jessamyn Read using Macromedia FreeHand 9 and Adobe Photoshop 6. The tip and warning icons were drawn by Christopher Bing. This colophon was written by Linley Dolby. The online edition of this book was created by the Safari production group (John Chodacki, Becki Maisch, and Madeleine Newell) using a set of Frame-to-XML conversion and cleanup tools written and maintained by Erik Ray, Benn Salter, John Chodacki, and Jeff Liggett.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值