This document was written by CS 290W TA David Corcoran and was last modified
This section describes how we will be using Perl to communicate and query an Oracle database using Oraperl. We will be using a READ ONLY account to access Oracle. This means we will be able to do queries only.
#!/usr/local/bin/perl do "cgi-lib.pl" || die "Cannot implement cgi-lib. \n"; &ReadParse; use Oraperl;This will make all the DBD::Oracle packages available to you. You will put this line at the top of your programs along with your do "cgi-lib.pl".
#!/usr/local/bin/perl do "cgi-lib.pl" || die "Cannot implement cgi-lib \n"; &ReadParse; use Oraperl; $ENV{"ORACLE_HOME"} = "/opt/oracle/product/8.1.5";It is necessary that you include that at the beginning of your Perl code. Now you are ready to begin communication with the Oracle Server. Oraperl uses some basic functions to communicate with the Oracle Server. Below are a few of these:
&ora_login &ora_open &ora_fetch &ora_close &ora_logoff
You will need this handle to perform operations to the connection you
just made. Consider the following:
#!/usr/local/bin/perl
do "cgi-lib.pl" || die "Cannot implement cgi-lib \n";
&ReadParse;
use Oraperl;
$ENV{"ORACLE_HOME"} = "/opt/oracle/product/8.1.5";
$sUsername = "cs290w\@csdb";
$sPassword = "cs290w";
# We connect, login and get a handle to the connection
# in scalar $hConnection.
$hConnection = &ora_login("", $sUsername, $sPassword);
#!/usr/local/bin/perl
do "cgi-lib.pl" || die "Cannot implement cgi-lib \n";
&ReadParse;
use Oraperl;
$ENV{"ORACLE_HOME"} = "/opt/oracle/product/8.1.5";
$sUsername = "cs290w\@csdb";
$sPassword = "cs290w";
# We connect, login and get a handle to the connection
# in scalar $hConnection.
$hConnection = &ora_login("", $sUsername, $sPassword);
$hQuery = &ora_open($hConnection, "SELECT * FROM StarWars");
#!/usr/local/bin/perl
do "cgi-lib.pl" || die "Cannot implement cgi-lib \n";
&ReadParse;
use Oraperl;
$ENV{"ORACLE_HOME"} = "/opt/oracle/product/8.1.5";
$sUsername = "cs290w\@csdb";
$sPassword = "cs290w";
# We connect, login and get a handle to the connection
# in scalar $hConnection.
$hConnection = &ora_login("", $sUsername, $sPassword);
$hQuery = &ora_open($hConnection, "SELECT * FROM StarWars");
print "Content-type: text/html \n\n";
@asQueryReturn = &ora_fetch($hQuery);
while (@asQueryReturn != 0)
{
print "You may reach $asQueryReturn[0] at $asQueryReturn[1] \n";
@asQueryReturn = &ora_fetch($hQuery);
}
This example queries the table StarWars and returns every (*) value
from the table. It then goes into a loop which will continue until no
more rows are left in the table (that is, the size of @asQueryReturn
is 0). Let's take an example table and this previous search and
determine what will be printed.
Name | |
Hans Solo | solo@starwars.net |
Luke Skywalker | skywalker@starwars.net |
Jabba the Hut | jabba@pizzahut.com |
You may reach Hans Solo at solo@starwars.net
You may reach Luke Skywalker at skywalker@starwars.net
You may reach Jabba the Hut at jabba@pizzahut.com
#!/usr/local/bin/perl do "cgi-lib.pl" || die "Cannot implement cgi-lib \n"; &ReadParse; use Oraperl; $ENV{"ORACLE_HOME"} = "/opt/oracle/product/8.1.5"; $sUsername = "cs290w\@csdb"; $sPassword = "cs290w"; # We connect, login and get a handle to the connection # in scalar $hConnection. $hConnection = &ora_login("", $sUsername, $sPassword); $hQuery = &ora_open($hConnection, "SELECT Email FROM StarWars"); print "Content-type: text/html \n\n"; @asQueryReturn = &ora_fetch($hQuery); while (@asQueryReturn != 0) { print "Email Address: $asQueryReturn[0] \n"; @asQueryReturn = &ora_fetch($hQuery); } # Close the query. &ora_close($hQuery); # Logoff and Close Connection. &ora_logoff($hConnection);This example basically does the same thing as the previous except the SQL command is different. This time we only want to return the Email field in the table. So only one value will be returned each fetch. This one value will still be stored in the array. You will access it as the zero element of this array ... hence printing out every email address in the table.