Perl操作Oracle

一、 perl连接Oracle数据库

[plain]view plaincopy

  1. [oracle@oracle11gR2 perl_script]$ more connect.pl
  2. #!/usr/bin/perl
  3. #perl script used to connect to Oracle
  4. use strict;
  5. use DBI;
  6. my $tnsname="ora11gR2";
  7. my $username="scott";
  8. my $password="tiger";
  9. my $dbh=DBI->connect("dbi:Oracle:$tnsname", $username, $password) or die "Cannot conenct db: $DBI::errstr\n";
  10. print "I have connected to the Oracle database!\n";
  11. $dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";
  12. print "Disconnected from Oracle databae!\n";
  13. [oracle@oracle11gR2 perl_script]$ ./connect.pl
  14. I have connected to the Oracle database!
  15. Disconnected from Oracle databae!

二、向数据库插入数据

[plain]view plaincopy

  1. [oracle@oracle11gR2 perl_script]$ more insert.pl
  2. #!/usr/bin/perl
  3. # this code is used to insert data to Oracle Database
  4. use strict;
  5. use DBI;
  6. my $id = 2;
  7. my $name = "denver";
  8. my $dbh = DBI->connect("dbi:Oracle:ora11gR2", "test","test") or die " Cannot connect db: $DBI::errstr\n";
  9. my $sql = qq{INSERT INTO m VALUES(?,?)};
  10. my $sth = $dbh->prepare($sql);
  11. $sth->execute($id, $name);
  12. print "I have inserted the record!\n";
  13. $dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";
  14. [oracle@oracle11gR2 perl_script]$ ./insert.pl
  15. I have inserted the record!
  16. [oracle@oracle11gR2 perl_script]$

三、删除数据

[plain]view plaincopy

  1. [oracle@oracle11gR2 perl_script]$ more delete.pl
  2. #!/usr/bin/perl
  3. # Delete Data From Oracle Database
  4. use strict;
  5. use DBI;
  6. my $id=2;
  7. my $dbh = DBI->connect("dbi:Oracle:ora11gR2", "test", "test") or die "Cannot connect db: $DBI::errstr\n";
  8. my $sql = qq{DELETE FROM m WHERE id=$id};
  9. my $sth = $dbh->prepare($sql);
  10. $sth->execute();
  11. print "I have deleted the record!\n";
  12. $dbh->disconnect or warn "DB disconnect failed:$DBI::errstr\n";
  13. [oracle@oracle11gR2 perl_script]$ ./delete.pl
  14. I have deleted the record!

四、查询

[plain]view plaincopy

    1. [oracle@oracle11gR2 perl_script]$ more select.pl
    2. #!/usr/bin/perl
    3. # Here is an example code piece to select data from Oracle
    4. use strict;
    5. use DBI;
    6. my $host = "localhost";
    7. my $sid = "denver";
    8. my $dbh = DBI->connect("dbi:Oracle:ora11gR2", "test", "test") or die "Cannot connect db:$DBI::errstr\n";
    9. print "I have connected to the Oracle 11g R2 database!\n";
    10. my $sql = qq{SELECT id, name FROM m};
    11. my $sth = $dbh->prepare($sql);
    12. $sth->execute();
    13. my ($pid, $pname); #declare columns
    14. $sth->bind_columns(undef, \$pid, \$pname);
    15. print "The results are:\n\n";
    16. while ( $sth->fetch() ) { #fetch rows from DataBase
    17. print "ID:$pid, --- NAME:$pname\n";
    18. }
    19. $sth->finish();
    20. $dbh->disconnect or warn "DB disconnect failed: $DBI::errstr\n";
    21. [oracle@oracle11gR2 perl_script]$ ./select.pl
    22. I have connected to the Oracle 11g R2 database!
    23. The results are:
    24. ID:0, --- NAME:**e
    25. ID:1, --- NAME:**e
    26. [oracle@oracle11gR2 perl_script]$