For When You Can't Have The Real Thing
[ start | index | login ]
start > dave > experiments > Net Flows > 2007-10-31 > 2

2007-10-31 #2

Created by dave. Last edited by dave, 16 years and 179 days ago. Viewed 2,625 times. #1
[edit] [rdf]
labels
attachments

Operating on Postgresql databases in perl

See: >>http://structbio.vanderbilt.edu/chazin/wisdom/dbi_howto.html

To execute a basic SQL statement:

There are three steps to executing a basic SQL statement:

  • Prepare the statement:
This example is a statement that will retrieve the rows in a table called reference.
$sth = $dbh->prepare( "
SELECT reference.ref_num, type, reference.title, pub_year, summary
FROM reference
");
if ( !defined $sth ) {
die "Cannot prepare statement: $DBI::errstrn";
}
  • Execute the statement:
This is done using the following line:
$sth->execute;
  • Fetch the rows into an array or variables:
This is an example that fetches the attributes from the rows into variables:
while ( ($ref_num, $type, $title, $pub_year, $summary ) = $sth->fetchrow()){
...intervening code…
}
This is an example that fetches the attributes into an array:
while ( @row = $sth->fetchrow()){
...intervening code…
}

After you are finished with the SQL statement, you should release the statement handle as follows:

$sth->finish;
no comments | post comment
This is a collection of techical information, much of it learned the hard way. Consider it a lab book or a /info directory. I doubt much of it will be of use to anyone else.

Useful:


snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt