Please send questions to
st10@humboldt.edu .
#!/usr/local/bin/perl -w
# ^NOTE --- I had to change the perl path on sorrel above
# so this would work!!!
#######################################################
# lect12_connect3
#
# playing with some more of DBI module?
#
# by Sharon Tuttle
#
# last modified: 11-09-04
#######################################################
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser carpout);
use DBI;
warningsToBrowser(1); # activate warnings
# PRINT a form for user to react to
print header, "\n";
print start_html(-title=>'lect12_connect3 page'), "\n";
print h1('lect12_connect3 page'), "\n";
print start_form, "\n",
br, br, "\n",
"Oracle username: \n",
textfield(-name=>'oracle_username',
-size=>15,
-maxlength=>15), "\n",
br, br, "\n",
"Oracle password: \n",
password_field(-name=>'oracle_password',
-size=>10,
-maxlength=>10), "\n",
br, br, "\n",
"Which table's contents should be displayed: \n",
textfield(-name=>'table_name',
-size=>30,
-maxlength=>30), "\n",
br, br, "\n",
checkbox(-name=>'show_tables',
-label=>'Show names of all tables',
-value=>'yes'), "\n",
br, br, "\n",
submit(-value=>'Submit'), "\n",
endform, "\n";
print hr, "\n";
# if user has hit submit, try to connect to the database and
# show the specified table's contents
if (param())
{
# set environment variables needed for Oracle database
# software
$ENV{'ORACLE_HOME'} = '/apps1/oracle/product/9iAS/';
$ENV{'LD_LIBRARY_PATH'} = '/apps1/oracle/product/9iAS/lib';
# try to connect to database named student reachable with the
# above environment informaton, for the account username and
# account password given, for an Oracle database
# (and remember --- what you connect, REMEMBER TO DISCONNECT!!!
my $username = param('oracle_username')
or die "Must enter an Oracle username!\n";
my $password = param('oracle_password')
or die "Must enter an Oracle password!\n";
my $db_handle = DBI->connect('dbi:Oracle:student',
"$username", "$password")
|| die "Database connection not made: $DBI::errstr";
# did user ask for the contents of a particular table?
if (param('table_name'))
{
my $table = param('table_name');
my $sql_cmd = "select * from $table";
# feed the SQL statement to the database handle
# with a statement handle; first, need to create
# this statement handle object:
my $stmt_handle = $db_handle->prepare( $sql_cmd )
or die $db_handle->errstr . "\n";
# now, execute the SQL command using the $stmt_handle object's
# execute() method
$stmt_handle->execute();
# how can I get the results? Well, one option is to call the
# fetchrow_array() method of the $stmt_handle object...
my @row_values;
# quick'n'sleazy table output --- yes, an HTML table would
# be slicker...
print hr, "\n",
"<PRE>\n",
br, "\n";
# grab the column names in this resulting relation/table
# my @col_names = $stmt_handle->{NAME};
# my $num = @col_names;
# $num--;
# foreach (0 .. $num)
# {
# printf "%10s", $col_names->[$_];
# }
# print "\n";
# print "--------------------------------------------------------------\n";
my $num_cols = $stmt_handle->{NUM_OF_FIELDS};
print "result has $num_cols columns\n\n";
while (@row_values = $stmt_handle->fetchrow_array())
{
foreach (@row_values)
{
# because a NULL database cell value is seen as
# as undefined in this array;
if (defined($_))
{
printf "%10s", $_;
}
else
{
print " NULL";
}
}
print "\n";
}
print "</PRE>\n";
# IF you are not reading all of the rows of the executed statement,
# I am told that is is good to call finish for your statement
# handle BEFORE disconnecting the database handle.
# SO, consider this a course style standard, too.
#
# (I'm just playing it safe here...)
$stmt_handle->finish;
print hr, "\n";
}
# did user ask for the names of all tables?
if (param('show_tables') eq "yes")
{
my $stmt_handle = $db_handle->prepare( 'select table_name
from user_tables' )
or die $db_handle->errstr . "\n";
$stmt_handle->execute();
my $next_tbl;
while ($next_tbl = $stmt_handle->fetchrow_array())
{
print "$next_tbl", br, "\n";
}
$stmt_handle->finish;
print hr, "\n";
}
# DON'T FORGET THIS! Not only is it POOR STYLE to do so, but
# could cause REAL Oracle performance problems if you leave
# it off!!!!!!!!!!!!!
$db_handle->disconnect;
}
print end_html, "\n";
# end of lect12_connect3.pl