Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: DBI selectall_arrayref

by davido (Cardinal)
on Sep 02, 2004 at 16:12 UTC ( [id://387969]=note: print w/replies, xml ) Need Help??


in reply to DBI selectall_arrayref

In addition to the comments regarding quoting of field names, I wanted to mention one other thing.

Just as you wouldn't dream of opening a file without checking the return value of open to see that the open was successful, you shouldn't just assume that your database operations are successful either, without proper error checking. For example, when you connect to the database:

$dbh = DBI->connect($data_source, $username, $password) or die $DBI::errstr;

Likewise, you need to error check after executing your selectall_arrayref(). This is explained in the POD:

If "RaiseError" is not set and any method except fetchall_arrayref fails then selectall_arrayref will return undef; if fetchall_arrayref fails then it will return with whatever data has been fetched thus far. You should check $sth->err afterwards (or use the RaiseError attribute) to discover if the data is complete or was truncated due to an error.

Also, maybe I'm not getting it, but when you call the connect() method, according to the DBI docs, the install_driver() method is called implicitly if needed. I don't think there's any need to call it yourself, and in fact, if you do call it yourself, calling it after attempting to connect is too late; you would need that driver to be installed before you can connect. I haven't run into any need to use the install_driver() method, so I could be way off, but it seems wrong to be installing the driver after trying to connect to the database.


Dave

Replies are listed 'Best First'.
Re^2: DBI selectall_arrayref
by jeffa (Bishop) on Sep 02, 2004 at 17:18 UTC

    I find it almost ironic that you mention error checking each query when you quote "If 'RaiseError' is not set ..."

    Well, why not just set RaiseError and never need worry about explicitly die'ing yourself? Barring that you are underneath miles of framework and child processes, of course -- but for most everyday uses, this is all you need do:

    my $dbh = DBI->connect($ds, $user, $pass, {RaiseError => 1});
    Look into HandleError as well.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      My point is that some error checking needs to be done, and none was being done. If the method is the RaiseError flag, so be it. But something's got to be in place or you'll never know if the DB is connecting, if the statement handles are being prepared and executed, and so on. The irony is that I discussed the more common Perl idiom (or die ...) instead of the more DBI'ish idiom (RaiseError).


      Dave

        ... or you'll never know if the DB is connecting

        Technically speaking, well yes, you could know if the DB is connecting without causing the script to die. If connect() fails, a warning via STDERR is emitted but your script still runs. By specifying {RaiseError => 1}, DBI will, by default, "throw" a die for you, but you can still see why the connect failed without having to explicitly or die $DBI::errstr or use {RaiseError => 1}.

        Finally, telling a "newbie" to go back and add error checking usually results in statements like "I just want to get it to work." That's why i recommend {RaiseError => 1} up front, you only need to add one line of code for error checking across the board. I'm just nitpicking Dave ... you and your point for some error checking are gold. :)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re^2: DBI selectall_arrayref
by THuG (Beadle) on Sep 02, 2004 at 17:12 UTC

    The error checking is coming. I just want to get it to work. Thanks, though.

    As to the driver, I don't know. It was in one of the documents as an example, after the connect, even. I wasn't sure what it did, but I didn't want to mess with that, yet. I now see that removing that line doesn't have any obvious affect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-19 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found