This is a sample perl script to extract some info from a database using perl. It runs a simple SQL statement and displays some values
from the table.
the SQL Table used in the perl example is
Group_svr table |
---|
group_id |
oms_group_id |
active_flag |
weight factor |
crea_tmptmp |
crea_user_id |
load_on_gs |
log count |
open_order_count |
upd_tmstmp |
upd_user_id |
#!/usr/local/bin/perl # #perform an extract from a database and display some entries #initial decleration use DBI; $database_vendor="Oracle"; #eg. Oracle, mysql $database_instance=""; #set this is there is nore than one db instance $username="neville"; $password="33!=mysql"; #initialise DB connection $dbh = DBI->connect ("dbi:$database_vendor:$database_instance", "$username", "$password") || die "ERROR: cannot connect to database...$ DBI::errstr \n"; #do the sql bit $group="G020"; $sqlstatement="select * from group_svr where group_id='$group'"; $sth = $dbh->prepare($sqlstatement); $sth->execute || die "ERROR: cannot get sql results ...$DBI::errstr \n"; #display the results print "Results of the sql select are:- \n\n"; while ( @row =$sth->fetchrow()) { $group_id=$row[0]; $oms_group_id=$row[1]; $upd_user=$row[10]; print "Group Id: $group_id OMS: $oms_group_id USER: $upd_user \n"; } #display a message if there are no matching entries. #we have to perform this after the while loop as some databases #dont know how may rows have been selected until the #while loop has been processed. if ($sth->rows == 0) { $print "There are no mathing records. \n"; } #clean up and close $sth->finish; #this says you are finished with the query $dbh->disconnect;