测试与开发

perl连接oracle数据库

上一篇 / 下一篇  2014-05-09 14:58:35 / 个人分类:perl

#!/usr/bin/perl -w

 

use strict;

use DBI;

 

my $dbh;

my $sth;

my $rv;

my $in_caps;

my @count;

 

eval

{

   # $dbh = DBI->connect("DBI:数据库类型(oraclemysql:服务名,用户名,密码,RaiseError=>1);

   $dbh = DBI->connect("DBI:Oracle:***", "***", "***", {RaiseError=>1,AutoCommit=>0});

   if (!defined $dbh)

   {

       exit(-1);

   }

   

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

   if (!defined $sth)

   {

       exit(-2);

   }

 

   my $rv = $sth->execute;

   if (!defined $rv)

   {

       exit(-3)

   }

   

   @count = $sth->fetchrow_array;   

   $in_caps = $count[0];

   $sth->finish;

 

   $dbh->disconnect;

 

      print "$in_caps\n";

 

};

 

if ($@)

{

   print $@;

}


可移植的DBI方法:
connect
建立到一个数据库服务器的连接
disconnect
断开数据库服务器的连接
prepare
准备执行一个SQL语句
execute
执行准备好的语句
do
准备并执行一个SQL语句
quote
加引号于要插入的字符串或BLOB
fetchrow_array
作为一个字段数组取出下一行
fetchrow_arrayref
作为一个字段的引用数组取出下一行
fetchrow_hashref
作为一个哈希表的引用取出下一行
fetchall_arrayref
作为一个字段数组取出所有数据
finish
完成一条语句并且让系统释放资源
rows
返回受影响的行数
data_sources
返回可在localhost上得到的数据库的数组
ChopBlanks
控制fetchrow_*方法是否剥去空格
NUM_OF_PARAMS
在准备的语句中的占位(placeholder-参数)的数目
NULLABLE
其列可以是NULL
trace
执行调试跟踪
##########################################################################
$dbh
数据库句柄
$sth
语句句柄
$rc
返回代码(经常是一个状态)
$rv
返回值(经常是一个行数)
##########################################################################
①connect($data_source, $username, $password)
使用connect方法使得一个数据库连接到数据源。$data_source值应该以DBI:driver_name:开始。以DBD::mysql驱动程序使用connect的例子:
$dbh = DBI->connect("DBI:mysql:$database", $user, $password);
$dbh = DBI->connect("DBI:mysql:$database:$hostname",
$user, $password);
$dbh = DBI->connect("DBI:mysql:$database:$hostname:$port",
$user, $password);


②disconnect
disconnect
方法从数据库断开数据库句柄。它一般就在你从程序退出之前被调用。范例:
$rc = $dbh->disconnect;
prepare($statement)
准备一条由数据库引擎执行的SQL语句并且返回语句句柄($sth),你可以使用它调用execute方法。一般地你借助于prepareexecute来处理SELECT语句(和类SELECT语句,例如SHOWDESCRIBEEXPLAIN)。范例:
$sth = $dbh->prepare($statement)
or die "Can't prepare $statement: $dbh->errstr/n";
③execute
execute
方法执行一个准备好的语句。对非SELECT语句,execute返回受影响的行数。如果没有行受影响,execute返回"0E0"

TAG:

 

评分:0

我来说两句

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 24040
  • 日志数: 28
  • 建立时间: 2008-09-22
  • 更新时间: 2021-02-23

RSS订阅

Open Toolbar