perl连接oracle测试实例

前端之家收集整理的这篇文章主要介绍了perl连接oracle测试实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下载地址: http://www.cpan.org
DBI::Oracle 、 DBD::Oracle下载并安装
如果你的linux可以连接网络,执行命令:
#wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.622.tar.gz
#wget http://search.cpan.org/CPAN/authors/id/P/PY/PYTHIAN/DBD-Oracle-1.50.tar.gz
下载到当前目录。

DBI:

[root@pc01 tools]# tar xvfz DBI-1.625.tar.gz 
[root@pc01 tools]# cd DBI-1.625


[root@pc01 DBI-1.625]# perl Makefile.PL
[root@pc01 DBI-1.625]# make
[root@pc01 DBI-1.625]# make test
[root@pc01 DBI-1.625]# make install
[root@pc01 DBI-1.625]# 


DBD:

 DBD::Oracle::模块的安装
1) 设置环境变量
# export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
# export ORACLE_USERID='system/xxxxxx@xxxx'
# export PATH=$ORACLE_HOME/bin:$PATH
# export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
2) 安装DBD::Oracle模块
[root@pc01 tools]# tar xvfz DBD-Oracle-1.60.tar.gz 
[root@pc01 tools]# cd DBD-Oracle-1.60
[root@pc01 DBD-Oracle-1.60]# perl Makefile.PL
[root@pc01 DBD-Oracle-1.60]# make
[root@pc01 DBD-Oracle-1.60]# make test
[root@pc01 DBD-Oracle-1.60]# make install




连接实例:

#!/usr/bin/perl 
use DBI; 
$dbname="xxxx"; 
$user="system"; 
$passwd="xxxxxx"; 
$dbh=""; 
$dbh = DBI->connect("dbi:Oracle:$dbname",$user,$passwd) or die "can't connect to database "; 
$sth=$dbh->prepare("select * from user_tables"); 
$sth->execute; 
while (@recs=$sth->fetchrow_array) { 
print $recs[0].":".$recs[1]."\n"; 

$dbh->disconnect; 
print "hello \n"; 
pc197:/home/oracle>perl con.pl
LOGMNR_SESSION_EVOLVE$:SYSAUX
LOGMNR_GLOBAL$:SYSAUX
LOGMNR_GT_TAB_INCLUDE$:
LOGMNR_GT_USER_INCLUDE$:
LOGMNR_GT_XID_INCLUDE$:
LOGMNR_UID$:SYSAUX
LOGMNRGGC_GTLO:SYSTEM
LOGMNRGGC_GTCS:SYSTEM
LOGMNRC_DBNAME_UID_MAP:SYSAUX
LOGMNR_LOG$:SYSAUX


$ more connect.pl 
#!/usr/bin/perl
#perl script used to connect to Oracle
use strict;
use DBI;


my $tnsname="xxxx";
my $username="system";
my $password="xxxxxx";


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";


pc197:/home/oracle>perl con2.pl 
I have connected to the Oracle database!
Disconnected from Oracle databae!

pc197:/home/oracle>

测试成功。

猜你在找的Perl相关文章