I am new to Perl and am trying to create a script that connects to my Oracle SQL Server database and returns the results of a table query via email. I am having trouble with the database connection part. Any ideas where I should start? Any example code would be greatly appreciated. Thanks,
3 Answers
Below is some sample code to get you connected to Oracle using the Perl DBI module. Adjust database name, username, and password as necessary.
#!/usr/bin/perl
use strict;
use DBI;
my $dbName = 'mydb';
my $username = 'username';
my $password = 'password';
my $options = { RaiseError => 1 };
my $dbh = DBI->connect("dbi:Oracle:${dbName}", $username, $password, $options);
$dbh is a database handle that you can use to execute all the queries that you like. See the DBI documentation page at CPAN for a concise description of the methods available.
3 Comments
use DBI; line. Please see the updated answer. Also bear in mind that my answer assumes that you already have installed Oracle client libraries and Perl module DBD::Oracle.First you should be aware of TNSNAMES.ORA file, which has predefined connections in form of
ORA11 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.0)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ORA12)
)
)
(check for above connection on server host if you happen to be on different machine)
Now you can use ORA11 as db name
my $DB = DBI->connect(
"dbi:Oracle:",
"USER/PASSWORD\@ORA11",
"",
{
# ChopBlanks => 1,
# AutoCommit => 0,
},
);
or use complete connection string instead of ORA11:
"USER/PASSWORD\@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.0)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORA12)))"
More connection options can be found in DBD::Oracle
Comments
I have a few dozen scripts that perform Oracle, Sybase, and MS SQL queries; even some scripts that perform queries on multiple databases and combine results, or build a query for one database based on result of a prior query ... but after many days of trying to get Perl libraries to play well.
For those of us that are real hacks, we start by using the Oracle SQL command-line client sqlplus.exe, which makes this approach easy, but far from pretty:
my $run_sql = 'sqlplus.exe -s DBuser/DBpwd@DBname < SQLfile.sql';
my $SQLfile = "temp.sql";
sub GET_EMP_LIST
{
my $status = $_[0];
my $sql_text = "
set linesize 150
set pagesize 0
set numf 99999999999
set feedback off
SELECT
EMP.FIRST || ',' ||
EMP.LAST || ',' ||
EMP.PHONE || ',' ||
EMP.SALARY
FROM
PERSONNEL.EMPLOYEES EMP
WHERE
(EMP.STATUS = '$status')
\;";
open (SQL, $FileOpenWrite, "$SQLfile");
print SQL $sql_text;
close (SQL_TEXT);
my @Results = "$run_SQL";
unlink $SQLfile;
return @Results;
}
#MAIN
@Employees = GET_EMP_LIST "Active";
for (@Employees)
{
my $temp = chomp $_;
$temp =~ s/\s+//g; #get rid of white spaces
my ($FIRST, $LAST, $PHONE, $SALARY) = split /,/, $temp;
.... do something with it ....
}
Like I say, far from pretty, but quick and easy, and using SQL query tools, like TOAD, you can generate the SQL in a drag & drop program before you integrate into your script.
I know many folks will say this is a terrible solution, but we pull in data that includes hundreds of thousands of lines of data.
DBD::Oraclewas so hard to build I ended up routing the thing throughDBD::ODBCinstead (which involves even more configuration far removed from the perl script). Oh how I wished forDBD::Oracle::PurePerl