in reply to Toggling dbi attribute within a connection?

Are you sure setting RaiseError to 0 isn't working? Did you also unset PrintError? What is the DBD and what is the error?
  • Comment on Re: Toggling dbi attribute within a connection?

Replies are listed 'Best First'.
Re^2: Toggling dbi attribute within a connection?
by JupiterCrash (Monk) on May 12, 2005 at 16:04 UTC
    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
      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
      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.