One, Perl connect Oracle Database
[Plain]View Plaincopy
- [Email protected] 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 has connected to the Oracle database!\n";
- $DBH->disconnect or warn "DB disconnect failed: $DBI:: errstr\n";
- Print "Disconnected from Oracle databae!\n";
- [Email protected] perl_script]$./connect.pl
- I have connected to the Oracle database!
- Disconnected from Oracle databae!
Ii. inserting data into the database
[Plain]View Plaincopy
- [Email protected] perl_script]$ more insert.pl
- #!/usr/bin/perl
- # This code was used to the 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";
- [Email protected] perl_script]$./insert.pl
- I have inserted the record!
- [Email protected] perl_script]$
Third, delete data
[Plain]View Plaincopy
- [Email protected] 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";
- [Email protected] perl_script]$./delete.pl
- I have deleted the record!
Iv. Inquiries
[Plain]View Plaincopy
- [Email protected] perl_script]$ more select.pl
- #!/usr/bin/perl
- # Here's 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 has 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 The rows from DataBase
- Print "ID: $pid,---NAME: $pname \ n";
- }
- $sth->finish ();
- $DBH->disconnect or warn "DB disconnect failed: $DBI:: errstr\n";
- [Email protected] perl_script]$./select.pl
- I have connected to the Oracle 11g R2 database!
- The results are:
- id:0,---name:**e
- Id:1,---name:**e
- [Email protected] perl_script]$