1.安装DBI和DBD::Oracle 模块。
下载地址:http://search.cpan.org/
搜索DBI,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
下载到当前目录。
2.安装
这里主要都是源码安装。
解压到目录:DBI-1.622
#tar –zxvf DBI-1.622.tar.gz
安装可以查看README文档。
一般步骤为
DBD::Oracle安装与DBI相同。
3.
配置环境变量
# export ORACLE_HOME=/u01/oracle
# export ORACLE_USERID='scott/tiger@ora11gR2' # export PATH=$ORACLE_HOME/bin:$PATH # export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH上面的方法只在当前登录环境有效,重新登陆后还需要重新配置。
这里建议的方法是使用Oracle 用户。或者将Oracle用户下.bash_profile文件拷贝到root目录下。
注意:LD_LIBRARY_PATH一定要修改,否则会报如下错误:
install_driver(Oracle) failed: Can't load '/usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/auto/DBD/Oracle/Oracle.so' for module DBD::Oracle: libclntsh.so.10.1: cannot open shared object file: No such file or directory at /usr/lib/perl5/5.8.5/i386-linux-thread-multi/DynaLoader.pm line 230.
4.数据库连接简单示例:
点击(此处)折叠或打开
- #!/usr/bin/perl -wuse strict;use warnings;use DBI;my $dbname="orcl";my $user = "omc";my $passwd = "omc";my $dbh=DBI->connect("dbi:Oracle:$dbname",$user,$passwd) or die "can't connect to database";my $sth = $dbh->prepare("select count(*) from fx_alarm");$sth->execute;my $count = $sth->fetchrow_array;$sth->finish();#Indicates to DBI that you are finished with the statementprint $count."\n";$dbh->disconnect;print "hello\n";