#!/usr/bin/perl -w ####################################### # print out the "sample" table from the msie02 database. # This one isn't a .cgi; it just runs from the command line. ######################################## use DBI; my ($user, $dbname, $table, $passwd) = ('msie02', 'msie02', 'sample', 'olymp.02' ); my $attributes = { RaiseError => 1, # Errors throw exceptions, i.e. die. AutoCommit => 0, # Use transactions, i.e. commit/rollback }; my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", $user, $passwd, $attributes) or die(" couldn't connect: $DBI::err "); eval { print "The table '$table' look like this: \n"; print " name | age \n"; my $allrows = $dbh->selectall_arrayref("SELECT * FROM $table;"); print " " . "+"x6 . " | " . "+"x4 . "\n"; foreach my $row (@$allrows) { printf(" %8s |%6i \n", @$row); } }; # "Catch" - handle error (if any) that aborted the eval{} if ($@) { warn "Transaction aborted : $@"; } print "Done. \n"; $dbh->disconnect();