Re: What is the meaning of the return value from a DBI do statement?
[email protected] (Robert Roggenbuck)
| Newsgroups | perl.dbi.users |
|---|---|
| Message-ID | <[email protected]> |
Larry W. Virden schrieb:
> I'm using DBI to interact with ORACLE . I have a number of statements
> - select, insert, update, and delete - that are executed depending on
> the logic.
>
> When I execute the $handle->do statement, is the return code a simple
In Your code example You do NOT use $handle->do()...
> true or false as to whether the statement worked, or does the value
> mean something more?
It means: "Returns the number of rows affected or undef on error. A return value
of -1 means the number of rows is not known, not applicable, or not available."
(http://search.cpan.org/~timb/DBI-1.609/DBI.pm#do). Your example code recognise
this correctly.
>
> I'd like to be able to determine how many rows the statement acted
> against.
>
> For example, in one part of the code, I tried doing this:
>
> $drc = $handle->prepare ("DELETE FROM MyTable WHERE EMPNO = ?") or
This must be written as
$oraProdDBH = $handle->prepare ("DELETE FROM MyTable WHERE EMPNO = ?") or
otherwise Your code will never work.
> do {
> print "DELETE prepare failed - $DBI::errstr\n";
> next;
> }
> $drc = $oraProdDBH->execute ($employee) or do {
> print "DELETE execute for $employee failed - $DBI::errstr\n";
> next;
> }
> if ($drc != 1) {
> if ($drc eq '0E0' or $drc == 0) {
> print "DELETE from MyTable for EMPNO $employee changed
> 0 rows\n";
> } else {
> print "DELETE from MyTable for EMPNO $employee changed
> $drc rows\n";
> }
> }
>
> thinking that I was logging useful information about the deletion.
> However, it seems like I frequentlly see the "changed 0 rows" message
> coming out at times when I should have seen a row deleted.
>
> Is this the method of testing to see if, in this case, the DELETE
> deleted any rows, and if so, how many? If not, what would be the way?
> And would this other way handle updates as well?
The problem is that You check the return value of $sth->execute() ...
Greetings
Robert