in reply to Re: Toggling dbi attribute within a connection?
in thread Toggling dbi attribute within a connection?

I did not also unset PrintError, but I just tried that, and still no luck. I am using the ODBC DBD at the time, but I believe I had also tried this with Oracle. Basically, I have code which wants RaiseError on for a prepare and execute. Then, I do NOT want it on for the fetches, because there is a fetch that could potentially throw an error. With RaiseError off (what I want) this code works:
# there is more to fetch, so set this high if ($sth->fetchrow_arrayref) { $extraFetch = 5000000; } else { $extraFetch = $numRowsDisplayed; }
This fetch could potentially be outside the boundaries of the rows returned. With RaiseError off, I don't die and the conditiom works fine. With RaiseError on, I get this error:

DBD::ODBC::st fetchrow_arrayref failed: (DBD: no select statement currently executing err=-1)

If RaiseError could be off at this point, all would be good.

However there are other areas in my code where I would have this same problem. Typically I will want to encapsulate a few statements in a transaction (with eval) and want RaiseError on for that. Then I will want it off, as there is code that follows which does things along the lines of

execute($sql) || handle the error

(where a die being thrown would never get into my "handle the error code)

Matt

Replies are listed 'Best First'.
Re^3: Toggling dbi attribute within a connection?
by jZed (Prior) on May 12, 2005 at 16:09 UTC
    This prints "1 error 3" for me with PostgreSQL, as I'd expect. I don't have ODBC handy to check.
    #!perl -w $|=1; use strict; use DBI; my $dbh=DBI->connect('dbi:Pg:dbname=test1'); $dbh->{PrintError}=0; $dbh->do('junk'); print "1"; eval { local $dbh->{RaiseError}=1; $dbh->do('junk'); print "2"; }; print " error " if $@; $dbh->do('junk'); print "3";
      Your sample code works for me as well (on ODBC).

      So something is obviously awry with my code.

      Hmm! More to come... :)
      (and thanks again)

      Matt
        It's possible that DBD::ODBC doesn't (or can't?) override all ODBC errors i.e. that some errors are considered "always fatal". I suggest writing to the dbi-users@perl.org list.
Re^3: Toggling dbi attribute within a connection?
by jZed (Prior) on May 12, 2005 at 16:30 UTC
    if ($sth->fetchrow_arrayref) { $extraFetch = 5000000; } else { $extraFetch = $numRowsDisplayed; }
    You might want to check for $sth->{Active} instead, it should be true if there is more to fetch and false otherwise.