Monday, August 23, 2010

Perl example - db connection

use DBI;
use strict 'vars';

my $dbh = DBI->connect("DBI:Oracle:TEST", "mjd", "gandalf",
{ RaiseError => 1 });
my $sth = $dbh->prepare("SELECT surname, givenname, city
FROM authors
WHERE id = ?");

while (1) {
print "Enter author's ID #: ";
my $id = ;
chomp $id;
last if $id == 0;
$sth->execute($id);
while (my $row = $sth->fetchrow_hashref) {
printf "%s %s from %s.\n",
$row->{surname}, $row->{givenname}, $row->{city};
}
$sth->finish;
}
$dbh->disconnect;

No comments: