Home > Perl Scripts > Sample Perl Script to connect Oracle DB

Sample Perl Script to connect Oracle DB

February 25, 2011 Leave a comment Go to comments

#!/usr/bin/perl
# Run under Oracle Perl, for DBI.
BEGIN {
die “ORACLE_HOME not set\n” unless $ENV{ORACLE_HOME};
unless ($ENV{OrAcLePeRl}) {
$ENV{OrAcLePeRl} = “$ENV{ORACLE_HOME}/perl”;
$ENV{PERL5LIB} = “$ENV{PERL5LIB}:$ENV{OrAcLePeRl}/lib:$ENV{OrAcLePeRl}/lib/site_perl”;
$ENV{LD_LIBRARY_PATH} = “$ENV{LD_LIBRARY_PATH}:$ENV{ORACLE_HOME}/lib32:$ENV{ORACLE_HOME}/lib”;
exec “$ENV{OrAcLePeRl}/bin/perl”, $0, @ARGV;
}
}

use strict;
use warnings;

use DBI;
my (@Table);
my($username, $password) = (‘vi’, ‘vi’);

my $dbh = DBI->connect(“dbi:Oracle:$ENV{ORACLE_SID}”, $username, $password) or die;
my $sql = qq{ SELECT tname FROM tab where rownum < 10};
my $sth = $dbh->prepare( $sql );
$sth->execute();
while ( @Table = $sth->fetchrow_array(  ) ) {
print “Table Name: @Table\n”;
}
[oracle@rh55-64b ~]$

Reference : http://www.orafaq.com/wiki/Perl

Advertisements
Categories: Perl Scripts
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: