• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

使用Perl管理Oracle数据库

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、连接数据库

#!/usr/bin/perl
use strict;
use DBI;

my $tnsname = "DB234";
my $username = "SCOTT";
my $password = "TIGER";

my $dbh = DBI->connect("dbi:Oracle:$tnsname",$username,$password) or die "Cannot connect db : $DBI::errstr\n";

print "I have connect to the Oracle database!\n";

$dbh->disconnect or warn "DB disconnect failed:$DBI::errstr\n";
connect oracle database

2、插入数据

#!/usr/bin/perl

use strict;
use DBI;

my $id = 9;
my $name = 'jame';

my $dbh = DBI->connect("dbi:Oracle:DB234","u1","u1") or die "Cannot connect db :$DBI::errstr\n";

my $sql = qq{INSERT INTO T1 VALUES(?,?)};
my $sth = $dbh->prepare($sql);

$sth->execute($id,$name);

print "I have inserted the record!\n";

$dbh->disconnect or warn "DB disconnect failed:$DBI::errstr\n";
insert into table data

3、删除数据

#!/usr/bin/perl
use strict;
use DBI;

my $id = 4;

my $dbh = DBI->connect("dbi:Oracle:DB234","u1","u1") or die "Cannot connect db : $DBI:: errstr \n";

my $sql = qq{ DELETE FROM T1 WHERE id=$id};
my $sth = $dbh->prepare($sql);

$sth->execute();

print "I have deleted the record!\n";

$dbh->disconnect or warn "DB disconnect failed:$DBI::errstr\n";
delete data

4、查询数据

#!/usr/bin/perl

use strict;
use DBI;

my $host = "localhost";
my $sid = "DB234";

my $dbh = DBI->connect("dbi:Oracle:DB234","u1","u1") or die "Cannot conect db $DBI::errstr\n";

print "I have connected to the Oracle 11g R2 Database!\n";

my $sql = qq{SELECT id,name FROM T1};
my $sth = $dbh->prepare($sql);

$sth->execute();

my ($pid,$pname);

$sth->bind_columns(undef,\$pid,\$pname);
print "The results are:\n\n";
while($sth->fetch()){
        print "ID:$pid ---- NAME:$pname\n";
}

$sth->finish();

$dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";
select data
[oracle@arcerzhang perl]$ ./select.pl
I have connected to the Oracle 11g R2 Database!
The results are:

ID:5 ---- NAME:lydiazhang
ID:6 ---- NAME:maryhu
ID:9 ---- NAME:jame
ID:1 ---- NAME:arcerzhang
ID:2 ---- NAME:arcerzhang
ID:3 ---- NAME:arcerzhang
search result:

5、使用perl调用存储过程

$dbh->do("BEGIN myPackage.myProcedure;END;");
CALL PROCEDURE

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
perl学习之:packageandmodule发布时间:2022-07-22
下一篇:
perl数组的长度与元素个数发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap