#!/usr/bin/perl
#perl script used to connect to Oracle
use strict;
use DBI;
#声明数据连接所需的参数
my $ip="127.0.0.1"; #oracle所在机器的ip
my $sid = "orcl"; #数据库实例
my $username = "liu"; #连接数据库的用户名
my $password = "weichuan"; #连接数据库的密码
#创建连接
my $dbh=DBI->connect("dbi:Oracle:host=$ip;sid=$sid", $username, $password) 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";
#perl script used to connect to Oracle
use strict;
use DBI;
#声明数据连接所需的参数
my $ip="127.0.0.1"; #oracle所在机器的ip
my $sid = "orcl"; #数据库实例
my $username = "liu"; #连接数据库的用户名
my $password = "weichuan"; #连接数据库的密码
#创建连接
my $dbh=DBI->connect("dbi:Oracle:host=$ip;sid=$sid", $username, $password) 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";
注意:转载需要注明出处和作者,作者:No. Liu