Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

DBI-do return value

by tune (Curate)
on Nov 29, 2001 at 03:16 UTC ( [id://128234]=perlquestion: print w/replies, xml ) Need Help??

tune has asked for the wisdom of the Perl Monks concerning the following question:

Hi every good monks in PerlGod!

I had some working with DBI, where I wanted to know how many rows the statement deleted.

my $nr = $dbh->do("DELETE FROM table WHERE date<'2001-10-01' LIMIT 150 +0") or die $dbh->errstr;
I needed to use LIMIT, because the table is huge, and running the statement without limit, would lock the table up for several minutes, which would cause me looking for a new job and a lot of money loss for my company :-)
So my script checks if $nr equals with the predefined row limit, which is 1500. If it is less it means that was the last portion, and exits.
First time it went well, but I wanted it to work from crontab, and when the case was occured the statement did not delete any records since there was no old records, so it (could) returned 0.
However it returned the string "0E0". What does it mean? I need a zero there! :)

Could anybody explain it to me? Thanks.

--
tune

Replies are listed 'Best First'.
Re: DBI-do return value
by AidanLee (Chaplain) on Nov 29, 2001 at 03:21 UTC
    0E0 is "Zero but True." So DBI is telling you that it deleted zero rows, successfully. If it didn't use 0E0 then the return value would be false, telling you that something went wrong...

    HTH

Re: DBI-do return value
by mpeppler (Vicar) on Nov 29, 2001 at 03:39 UTC
    0E0 means 0-but-true. This is so that do() can report 0 rows affected, but not cause your
    or die $dbh->errstr
    to be called.

      You could also use the DBI method $nr->rows();

      my $rows_affected=$nr->rows();

        You can't call rows() on a database handle, only on a statement handle, which of course means that you can't do
        $dbh->do(....); $rows = $dbh->rows(); # won't work!

        Michael

        That will only work if $nr is a statement handle. In the above example $nr was a row count, not a statement handle.
Re: DBI-do return value
by runrig (Abbot) on Nov 29, 2001 at 03:28 UTC
    '0E0' is special and can be used without warnings, AidenLee is right, but to clarify, the following works fine:
    use strict; use warnings; my $num = '0E0'; print "Done\n" if $num < 1500; print "Error\n" unless $num; # just prints 'Done'...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128234]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found