in reply to Re^2: Can I get the actual error for DBI->execute() ?
in thread Can I get the actual error for DBI->execute() ?

Unfortunately, not even that will work seeing as how $sth hasn't even been declared.


🦛

Replies are listed 'Best First'.
Re^4: Can I get the actual error for DBI->execute() ?
by SergioQ (Scribe) on Jun 21, 2024 at 18:18 UTC

    So the code below gave me the most detail. I tried checking for $dbHandle->err after the prepare statement but it returned nothing.

    And I just wanted to just be clear: setting RaiseError => 1 does cause the script to exit immediately, yes?

    Thanks for everyone's help.
    sub insertItem { (my $insertCom) = @_; $dbHandle=$db->prepare($insertCom); my $result = $dbHandle->execute(); if ($dbHandle->err || $dbHandle->errstr) { $logger->debug("err | $DBI::err | $DBI::errstr"); } return $result; }
      SergioQ wrote: And I just wanted to just be clear: setting RaiseError => 1 does cause the script to exit immediately, yes?

      Yes. You can also log any errors associated with the database handle without exiting by passing PrintError => 1 and warnings with PrintWarn => 1 to the DBI->connect() call. I generally don't want to make any changes to the database if any errors occur so I use AutoCommit => 0, and only $dbh->commit if everything passes within whatever constraints I set.

      Yes. setting RaiseError => 1 does cause the script to exit immediately, yes?

      I prefer to do it that way instead of checking for errors after each DB operation. In your code above, prepare can also throw an error. $dbHandle will be undef if there is an error and you should check for that if you don't set RaiseError.

      Note that execute sometimes returns a numerical value that has meaning, like perhaps the number of rows updated. execute behaves differently say for a SELECT statement where no information is provided. So a simple check of $result to detect an error won't work like it does for $dbHandle.

        Marshall wrote: So a simple check of $result to detect an error won't work like it does for $dbHandle.

        I'm pretty sure $sth->execute(...) returns undef if and only if there is an error. From perldoc DBI:

        An "undef" is returned if an error occurs. A successful "execute" always returns true regardless of the number of rows affected, even if it’s zero (see below).