Accessing Pervasive.SQL With Perl

Page Last Modified:

Home Links ODBC (DBI) Contact

DBI + ODBC - Fetching Examples 5 - using eval to catch errors

use strict;
use warnings;
use DBI;

eval {
print "Connecting to database...\n";
my $dbHandle = DBI->connect('dbi:ODBC:DEMODATA', '', '', {PrintError=>0});


print "Preparing query...\n";
my $stHandle = $dbHandle->prepare('SELECT Name,Description FROM Course ORDER By Name');

print "Running query...\n";
my $ret = $stHandle->execute();

print "Returning rows...\n";
while(my @returnRow = $stHandle->fetchrow_array())
{
print "Name: $returnRow[0], description: $returnRow[1]\n";
}

print "Disconnecting...\n";
$stHandle->finish();
$dbHandle->disconnect();

1;
}
or do {
print "Error: $DBI::err - $DBI::errstr\n"
};

print "Finished\n";

Instead of using the RaiseError attribute to fail instantly for us, we catch problems with eval and display whatever message we see fit. We are still not explicitly freeing resources if there is a problem.

Home Links ODBC (DBI) Contact

All content on this site is copyright
Neil Hughes 2010 - 2018