#!/usr/bin/perl
#
# eg6: use DBI to connect to a PostgreSQL database
#
use strict;
use warnings;

use DBI;

my $db   = "films";
my $host = "db.doc.ic.ac.uk";
my $port = 5432;
my $user = 'lab';
my $password = 'lab';

my $dbh = DBI->connect(
	    "dbi:Pg:dbname=$db;host=$host;port=$port",
	    $user, $password
	  ) || die "can't connect to $db as $user";

my $sth = $dbh->prepare("select * from films");
$sth->execute || die "Database error: " . $dbh->errstr;

while( my $record = $sth->fetchrow_hashref )
{
	print "Title:    $record->{title}\n";
	print "Director: $record->{director}\n";
	print "Origin:   $record->{origin}\n";
	print "Made:     $record->{made}\n";
	print "Length:   $record->{length}\n";
	print "-" x 30 . "\n";
}
$sth->finish;

$dbh->disconnect;
