Perl连接oracle数据库
#---------------------------------------------------------------# Project Name:
# >>perl连接oracle实验
#
# Program Description:
# >>This job is scheduled via Windows scheduler to run
# on demand to loading interfaces generated by 测试 System
#
# Usage: perl_oracle.pl
#
# Note:
# >>We loading following CLS tables:
# contacted_customer
#
# Author:LLL
# e-Mail:LLL@163.com
#
#---------------------------------------------------------------
require 'site_config.pl';
#!/usr/bin/perl
#perl script used to connect to Oracle
use strict;
use DBI;
#创建连接
my $dbh=DBI->connect("dbi:Oracle:host=$ip;sid=$orcl_sid_orcl", $orcl_user_uat, $orcl_pass_uat) or die "Cannot conenct oracle11g: $DBI::errstr\n";
#声明变量
my $deptno = 99;
my $dname = "denver";
my $loc = "test";
#插入数据
my $sql = qq{INSERT INTO dept VALUES(?,?,?)};
my $sth = $dbh->prepare($sql);
$sth->execute($deptno, $dname,$loc);
#删除数据
my $sql = qq{DELETE FROM dept WHERE loc='$loc'};
my $sth = $dbh->prepare($sql);
#$sth->execute();
print "--I have deleted the record!\n";
#查询数据
my $sql = qq{SELECT deptno, dname, loc FROM dept};
my $sth = $dbh->prepare($sql);
$sth->execute();
#my ($pid, $pname); #declare columns
$sth->bind_columns(undef, \$deptno, \$dname, \$loc);
print "The results are:\n\n";
while ( $sth->fetch() ) { #fetch rows from DataBase
print "ID:$deptno, --- NAME:$dname, --- LOC:$loc\n";
}
$sth->finish();
#修改数据
my $loc = "newddd";
my $sql = qq{update dept set loc='$loc' where deptno=$deptno};
my $sth = $dbh->prepare($sql);
$sth->execute();
#断开连接
$dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";
print "Disconnected from Oracle databae!\n";
页:
[1]