Home: Perl Programming Help: DBI:
err, state & errstr captured only on fetarray, not on execute



abandekar
New User

Aug 24, 2015, 5:15 AM


Views: 20203
err, state & errstr captured only on fetarray, not on execute

Hi All,

I am new to the perl world. I am trying to connect to a odbc data source, execute a procedure and fetch the o/p result set.

Now the procedure I am calling, I know, will error out (because of deliberate raiseeror in an attempt to test the error handling). But when I execute the script the error is captured not in $sth->execute(), instead at sth->fetcharray_hashref().

I am doing this with a non-standard ODBC driver (I mean ODBC driver of a non-relational database).

Below is the code snipped I am trying:

Code
#!/ms/dist/perl5/bin/perl5.14-64 
use strict;
use warnings;

use DBI;
use DBD::ODBC;
my $dbh = DBI->connect('dbi:ODBC:testing', '', '') or die "ERROR: Failed to connect, $DBI::errstr";

my $query = "exec testing.parent";
my $sth = $dbh->prepare($query) or die "ERROR: Failed prepare of stmt to fetch proc parms, $DBI::errstr";

my $temp = $sth->execute();
my $errstr = $sth->errstr();
print "ret $temp\n"; # pirnts -1
print "$errstr\n"; # Says $errstr is uninitialized

my $array = $sth->fetchrow_hashref(); # Throws error
$errstr = $sth->errstr();
print "$errstr\n"; # prints the intended error string

$sth->finish();
exit 0;


Is this a normal behavior? or is this problem with the non-standard odbc driver vendor provided?

As per my requirements, I have to capture the SQL Error after execute.

Please help.


FishMonger
Veteran / Moderator

Aug 24, 2015, 8:10 AM


Views: 20197
Re: [abandekar] err, state & errstr captured only on fetarray, not on execute

What you're seeing is normal behavior and should be expected.

There is a difference between a statement that can't be executed vs a statement that can be executed but deliberately raises an exception.

If it helps, think of it as the difference between perl's compile time vs runtime errors.


(This post was edited by FishMonger on Aug 24, 2015, 8:13 AM)