perl需要文件准备
1、下载DBI和DBD
wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.633.tar.gz
wget http://search.cpan.org/CPAN/authors/id/P/PY/PYTHIAN/DBD-Oracle-1.74.tar.gz
如果失效,可以去cpan:http://search.cpan.org/ 搜索下载
2、查看perl是否安装,未安装还得装perl
perl -v
3、安装DBI,应该先安装DBI否则DBD无法安装成功
解压缩后进压缩文件夹执行
perl Makefile.PL
make
make install
4、安装DBD-Oracle
tar -zxvf DBD-Oracle-1.74.tar.gz
如果root下进行安装出现错误如下,则表明没未设置环境变量ORACLE_HOME
perl Makefile.PL
Using DBI 1.605 (for perl 5.008005 on i386-linux-thread-multi) installed in /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBI/
Configuring DBD::Oracle for perl 5.008005 on linux (i386-linux-thread-multi)
Remember to actually *READ* the README file! Especially if you have any problems.
Trying to find an ORACLE_HOME
Your LD_LIBRARY_PATH env var is set to ''
The ORACLE_HOME environment variable is not set and I couldn't guess it.
It must be set to hold the path to an Oracle installation directory
on this machine (or a machine with a compatible architecture).
See the appropriate README file for your OS for more information.
ABORTED!
设置ORACLE_HOME为之前解压缩下载文件的目录
export ORACLE_HOME=/usr/local/src/instantclient_12_1
perl Makefile.PL
make
make install
5、验证结果
脚步如下:
db.pl :
#!/usr/bin/perl -w
use DBI;
my $tnsname="orcl";
my $dbUser="test";
my $dbPassword="test";
my $dbh = DBI->connect("dbi:Oracle:$tnsname",$dbUser,$dbPassword, {'RaiseError' => 1});
# Insert one row
my $rows = $dbh->do("INSERT INTO t (id) VALUES (1)");
# query
my $sth = $dbh->prepare("SELECT id FROM t");
$sth->execute;
while(my @ref = $sth->fetchrow_array ) {
print "$ref[0]\n";
}
$dbh->disconnect();