Monday, December 31, 2007

Simple Perl Script

#
# Simple perl script to connect to Oracle database using DBI::Oracle
#

#!/usr/bin/perl -w

# Get the count of the parameters
my $count=@ARGV;

# if paramters are not 3 exit
if ($count!=3) {
print "Usage: ./dbcon.pl [dbname] [user] [password]\n";
exit ;
}

use strict;
use DBI;

# Assign the parameter values to variables
my $db=$ARGV[0];
my $usr=$ARGV[1];
my $pass=$ARGV[2];

# Connect to database
my $dbh=DBI->connect("dbi:Oracle:$db",$usr,$pass)
or die "Connection failed DBI::errstr";


# Prepare the sql statement
my $sql="select name,open_mode,log_mode,database_role from v\$database";
my $sth=$dbh->prepare($sql);

# Execute the sql statement
$sth->execute();

# Fetch the data and dilpay
my @row=$sth->fetchrow_array;
print "Database Details\n";
print "Database Name: $row[0]\n";
print "Database Role: $row[3]\n";
print "Open Mode : $row[1]\n";
print "Log Mode : $row[2]\n";

# Disconnect from the database
$sth->finish();
$dbh->disconnect();

# Output

[oracle@db10g]$ chmod +x dbcon.pl
[oracle@db10g]$ ./dbcon.pl
Usage: ./4.pl [dbname] [user] [password]

[oracle@db10g]$ ./dbcon.pl db10g naikh naikh
Database DetailsDatabase
Name: DUPDB
Database Role: PRIMARY
Open Mode : READ WRITE
Log Mode : ARCHIVELOG

No comments: