Why am I getting 0E0 for the return value of the DBI "execute" method?
Asked Answered
O

1

5

I have written a sample Perl program to delete data from a database table.

This is the code I have written,

use DBI;

my $dbh = DBI->connect("DBI:Pg:host=192.168.12.23;port=5432;", "adhi");                                                                                
if ( $dbh ) {

    print "Connected successfully\n";

    my $exe = $dbh->prepare("delete from perl_test.test");
    my $res = $exe->execute();
    if ( $res ) {
        print "deleted the table successfully of rows: $res\n";
    }
}

If I have executed the above it should print successful message and then the number of rows deleted.

If the table was empty it was printing 0E0 instead of 0. I don't know how it is returning the value like this?

Can someone please explain me how it was working?

Osbourne answered 28/7, 2016 at 12:12 Comment(2)
You wrote "In that I have used the do method to execute the query" but your program doesn't use the do method so I deleted that sentence. Please also indent your Perl code properly so that it is readable, especially before asking for help with it.Sweatshop
Thanks Borodin .I will change it.Osbourne
D
11

It is done in this way to allow to test whether the operation was successful. The reason is that '0E0' (as a string) is a true value, but 0 is a false value in Perl. Therefore:

  1. you can test the return value in if to determine whether the operation was successful (true means success),

  2. but you can also use the return value as a number to know the exact number of deleted rows, since 0E0 evaluates to 0 when used as a number.

If you need just the number of deleted rows, you can use $res + 0 or $res * 1. But only after you have tested that the operation was successful.

Dogie answered 28/7, 2016 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.