Perl 连接 Oracle 示例
1.首先需要先下载3个模块 DBI-1.622.tar.gz DBD-Oracle-1.50.tar.gz Data-ShowTable-3.3.tar.gz (data目录中)2. 安装[root@jam
1.首先需要先下载3个模块
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!
大功告成
文中涉及的三个包我已经上传,详细看下面的地址:
免费下载地址在
用户名与密码都是
具体下载目录在 /2012年资料/9月/12日/Perl 连接 Oracle 示例
,