Perl Operations Oracle

Source: Internet
Author: User
Tags prepare perl script

One, Perl connect Oracle Database

[Plain]View Plaincopy
  1. [Email protected] 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 has 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. [Email protected] perl_script]$./connect.pl
  14. I have connected to the Oracle database!
  15. Disconnected from Oracle databae!


Ii. inserting data into the database

[Plain]View Plaincopy
  1. [Email protected] perl_script]$ more insert.pl
  2. #!/usr/bin/perl
  3. # This code was used to the 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. [Email protected] perl_script]$./insert.pl
  15. I have inserted the record!
  16. [Email protected] perl_script]$


Third, delete data

[Plain]View Plaincopy
  1. [Email protected] 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. [Email protected] perl_script]$./delete.pl
  14. I have deleted the record!


Iv. Inquiries

[Plain]View Plaincopy
    1. [Email protected] perl_script]$ more select.pl
    2. #!/usr/bin/perl
    3. # Here's 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 has 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 The 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. [Email protected] 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. [Email protected] perl_script]$
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.