Accessing Pervasive.SQL With Perl

Page Last Modified:

Home Links ODBC (DBI) Contact

DBI + ODBC - Fetching Examples 4 - catching errors using the code

use strict;
use warnings;
use DBI;

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

if(defined($dbHandle))
{
   print "Preparing query...\n";
   my $stHandle = $dbHandle->prepare('SELECT Name,Description FROM Course ORDER By Name');
   if(defined($stHandle))
   {
      print "Running query...\n";      
      my $ret = $stHandle->execute();
   
      if(defined($ret))
      {
         print "Returning rows...\n";
         while(my @returnRow = $stHandle->fetchrow_array())
         {
            print "Name: $returnRow[0], description: $returnRow[1]\n";
         }
      }
      else
      {
         print "Unable to run query\n";	      
      }
      
      $stHandle->finish();
   }
   else
   {
      print "Unable to create statement\n";
   }

   print "Disconnecting...\n";      
   $dbHandle->disconnect();
}
else
{
   print "Unable to create database handle\n";
}

print "Finished\n";

Now we explicitly check each call and make sure that any allocated resources are freed. We have to create our own error messages, which hopefully are a bit more detailed than this example, e.g. using $DBI::err and $DBI::errstr, and there is the risk of not checking every call, such as the fetchrow_array in the example above where it is assumed that no return value means no more rows left.

Home Links ODBC (DBI) Contact

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