Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Does fetchall_arrayref() ever return a NULL?

by GrandFather (Saint)
on May 06, 2022 at 02:29 UTC ( [id://11143613]=note: print w/replies, xml ) Need Help??


in reply to Does fetchall_arrayref() ever return a NULL?

From DBI's fetchall_arrayref section:

If called on an inactive statement handle, fetchall_arrayref returns undef.

There may be other stuff in the DBI documentation that may be of interest. Have you read it?

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
  • Comment on Re: Does fetchall_arrayref() ever return a NULL?

Replies are listed 'Best First'.
Re^2: Does fetchall_arrayref() ever return a NULL?
by hippo (Bishop) on May 06, 2022 at 10:28 UTC

    My attempt to reproduce this has failed.

    use strict; use warnings; use Test::More tests => 2; use DBI; use Data::Dumper; my $dbh = DBI->connect ('dbi:SQLite:dbname=:memory:', '', ''); my $sth = $dbh->prepare ('SELECT 1 = 1;'); $sth->execute; $sth->finish; ok ! $sth->{Active}, 'Handle is inactive'; my $res = $sth->fetchall_arrayref (); is $res, undef, 'Result is undef' or diag Dumper ($res); diag "DBI $DBI::VERSION";
    $ perl inactive.t 1..2 ok 1 - Handle is inactive not ok 2 - Result is undef # Failed test 'Result is undef' # at /tmp/inactive2.t line 16. # got: 'ARRAY(0x2c41050)' # expected: undef # $VAR1 = []; # DBI 1.643 # Looks like you failed 1 test of 2.

    On this apparently inactive handle it still returns an empty arrayref. Where am I going wrong?


    🦛

      I tried your test using MySQL and it gave the same result. But I checked the handle for errors and it produced "fetch() without execute()". So perhaps returning an empty arrayref is correct, as there was an error. Interesting that SQLite did not throw that error.

      You can't do any fetch after calling finish(), I don't think

        I'm not trying to perform a successful fetch - I'm trying to reproduce the documented behaviour. Hence calling finish first in order to ensure that the handle is inactive. And the fetch still doesn't return undef.


        🦛

      Thank for trying all that.

      In the end, I decided it would just be easier if I checked for null in both places and move on.

      checking for undef in the return, and no records when I check the array size

        Another option is to enable RaiseError, and then you get an exception instead of undef, and cleaner code to wrap up errors.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found