首页 > 代码库 > Perl操作Oracle
Perl操作Oracle
一、 perl连接Oracle数据库
[plain] view plaincopy
- [oracle@oracle11gR2 perl_script]$ more connect.pl
- #!/usr/bin/perl
- #perl script used to connect to Oracle
- use strict;
- use DBI;
- my $tnsname="ora11gR2";
- my $username="scott";
- my $password="tiger";
- my $dbh=DBI->connect("dbi:Oracle:$tnsname", $username, $password) or die "Cannot conenct db: $DBI::errstr\n";
- print "I have connected to the Oracle database!\n";
- $dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";
- print "Disconnected from Oracle databae!\n";
- [oracle@oracle11gR2 perl_script]$ ./connect.pl
- I have connected to the Oracle database!
- Disconnected from Oracle databae!
二、向数据库插入数据
[plain] view plaincopy
- [oracle@oracle11gR2 perl_script]$ more insert.pl
- #!/usr/bin/perl
- # this code is used to insert data to Oracle Database
- use strict;
- use DBI;
- my $id = 2;
- my $name = "denver";
- my $dbh = DBI->connect("dbi:Oracle:ora11gR2", "test","test") or die " Cannot connect db: $DBI::errstr\n";
- my $sql = qq{INSERT INTO m 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";
- [oracle@oracle11gR2 perl_script]$ ./insert.pl
- I have inserted the record!
- [oracle@oracle11gR2 perl_script]$
三、删除数据
[plain] view plaincopy
- [oracle@oracle11gR2 perl_script]$ more delete.pl
- #!/usr/bin/perl
- # Delete Data From Oracle Database
- use strict;
- use DBI;
- my $id=2;
- my $dbh = DBI->connect("dbi:Oracle:ora11gR2", "test", "test") or die "Cannot connect db: $DBI::errstr\n";
- my $sql = qq{DELETE FROM m 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";
- [oracle@oracle11gR2 perl_script]$ ./delete.pl
- I have deleted the record!
四、查询
[plain] view plaincopy
- [oracle@oracle11gR2 perl_script]$ more select.pl
- #!/usr/bin/perl
- # Here is an example code piece to select data from Oracle
- use strict;
- use DBI;
- my $host = "localhost";
- my $sid = "denver";
- my $dbh = DBI->connect("dbi:Oracle:ora11gR2", "test", "test") or die "Cannot connect db:$DBI::errstr\n";
- print "I have connected to the Oracle 11g R2 database!\n";
- my $sql = qq{SELECT id, name FROM m};
- my $sth = $dbh->prepare($sql);
- $sth->execute();
- my ($pid, $pname); #declare columns
- $sth->bind_columns(undef, \$pid, \$pname);
- print "The results are:\n\n";
- while ( $sth->fetch() ) { #fetch rows from DataBase
- print "ID:$pid, --- NAME:$pname\n";
- }
- $sth->finish();
- $dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";
- [oracle@oracle11gR2 perl_script]$ ./select.pl
- I have connected to the Oracle 11g R2 database!
- The results are:
- ID:0, --- NAME:**e
- ID:1, --- NAME:**e
- [oracle@oracle11gR2 perl_script]$
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。