After you install Perl, in a console window go to the Pear’s Bin subdirectory. Type:
C:\Perl\bin\ppm
PPM is a program that facilitates using Perl for the Internet and database.
You should get a ppm prompt PPM> at that prompt type:
PPM>install DBD-ODBC DBD-Oracle DBI
Note: install requires an Internet connection.
Enter y (yes) for each package name.
Check if all the packages are installed by issuing the following command:
PPM> query
It should return with information on the Oracle driver
Help gives a summery of PPM commands.
PPM> help
PPM> quit
Quits PPM.
Now create a DSN i.e., an ODBC System DSN (datasource) using the Oracle Driver.
To run the example given below name the data source ) Ora_scott
Example:
#! /c/perl/bin/perl
# Program name: ex5.pl
# Perl program accessing Oracle via ADO and ODBC
# 1st create a DSN named "ORA_scott" as previously described.
# Make sure your database server is running before trying the program
print "hi from $0\n";
print ("\t ADO with DSN \"ORA_scott\" on the Oracle server named \"liu\" \n");
print ("\t Execute: SELECT ename, empno, job, deptno FROM emp \n\n");
print("\t ** If connect fails and server is running, change ODBC Driver **\n");
print "\n";
use OLE;
$conn = CreateObject OLE "ADODB.Connection" || # Create ADO auto object
die "Error on ADO CreateObject: $!" ;
$conn->Open('ORA_scott' ); # Connect to DSN
if ( $conn->{State} != 1 ) { # 1 is adStateOpen , 0 is asStateClosed
die ("\t Connection Not Open. \n\t Make sure the server is started.\n\n");
}
$sql = "SELECT ename, empno, job, deptno FROM emp "; # SQL Statement to run
$rs = $conn->Execute($sql ); # Execute the query
print ("ename\t empno job\t\tdeptno\n");
while( !$rs->EOF() ) { # Fetch each row, one at a time
printf( "%-11s\t", $rs->Fields(ENAME)->Value );
print ( $rs->Fields(EMPNO)->Value, " " ); # Oracle ADO permits lower
printf( "%-9s\t", $rs->Fields(JOB)->Value ); # or uppercase column names
print ( $rs->Fields(deptno)->Value, "\t" );
print ("\n");
$rs->MoveNext;
}
$rs->Close;
$conn->Close;
# Last line of file
##On the web program
#! /c/perl/bin/perl
# Program name:
ex5.pl
# Perl program accessing Oracle via ADO and ODBC
# 1st create a DSN named "ORA_scott" as previously
described.
# Make sure your database server is running before trying
the program
#print "hi from $0\n";
#print ("\t ADO with DSN \"ORA_scott\" on the
Oracle server named \"liu\" \n");
#print ("\t Execute: SELECT ename, empno, job, deptno
FROM emp \n\n");
#print("\t ** If connect fails and server is running,
change ODBC Driver **\n");
print "\n";
use OLE;
$conn = CreateObject
OLE "ADODB.Connection" || #
Create ADO auto object
die
"Error on ADO CreateObject: $!" ;
$conn->Open('ORA_scott' ); # Connect to DSN
if ( $conn->{State} != 1 ) { # 1 is adStateOpen , 0 is
asStateClosed
die ("\t
Connection Not Open. \n\t Make sure the server is started.\n\n");
}
$sql = "SELECT ename, empno, job, deptno FROM emp
"; # SQL Statement to run
$rs = $conn->Execute($sql ); # Execute the query
print("Content-Type: text/html\n\n");
print("<HEAD><TITLE>Results</TITLE></HEAD>\n");
print("<BODY>\n");
print
("<H1>ename\t empno job\t\tdeptno</H1>\n");
while(
!$rs->EOF() ) { # Fetch each row,
one at a time
print("<P>");
printf(
"%-11s\t", $rs->Fields(ENAME)->Value );
print (
$rs->Fields(EMPNO)->Value, " " ); # Oracle ADO permits lower
printf(
"%-9s\t", $rs->Fields(JOB)->Value ); # or uppercase column names
print (
$rs->Fields(deptno)->Value, "\t" );
print ("\n");
$rs->MoveNext;
}
print("</BODY>\n");
print("</HTML>\n");
$rs->Close;
$conn->Close;
# Last line of file