in reply to Perl DBI problem

Hi matth,

In addition to diotalevi's reply above I'd add the following. If you had used '$DBI::errstr' that would have given you more insight into what was going wrong. Below is a little self contained programme that includes using strict and warnings, which is generally considered "good practice" round here.

#!/usr/bin/perl -w use strict; use DBI; my $dbh = DBI->connect('DBI:mysql:genome', 'u', 'p') || die $DBI::er +rstr; my $sth = $dbh->prepare("SELECT foo FROM bar WHERE baz = 4"); $sth->execute() || die $DBI::errstr; while (my ($foo) = $sth->fetchrow()) { # Do something with $foo eg: print "foo = $foo\n"; } $sth->finish(); $dbh->disconnect() || warn $DBI::errstr;

Give that a try and see what happens, hope it helps.
barrd

Replies are listed 'Best First'.
(jeffa) 2Re: Perl DBI problem
by jeffa (Bishop) on Jun 14, 2003 at 13:21 UTC
    But why use $DBI::errstr in such a simple situation when you can turn the RaiseError attribute on instead? :)
    my $dbh = DBI->connect( qw(DBI:vendor:database:host user pass), {RaiseError => 1}, ); my $sth = $dbh->prepare('get me some records and stuff'); $sth->execute;

    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)
    
      Hi jeffa,

      Fair enough, I don't use that method as I prefer what might be known as the long hand approach. You are probably correct. I haven't tried RaiseError so if you or anyone else could answer the following I'd be grateful (and may even change my mind as to how to go about doing error exceptions ;).

      My understanding is this: $DBI::errstr method returns the native database engine error message from the last driver function called. Does using RaiseError do exactly the same thing? And AFAIR RaiseError is used to "force errors" - will it implicitly 'die' on any error?

      I'm just another monk trying to learn from my peers and betters.
      barrd

        The "long hand" approach? Doesn't sound very Perlish. ;)

        Anyway, hate to do this to you, but you should have RTFM'ed:

        `RaiseError' (boolean, inherited) The `RaiseError' attribute can be used to force errors to raise exceptions rather than simply return error codes in the normal way. It is "off" by default. When set "on", any method which results in an error will cause the DBI to effectively do a `die("$class $method failed: $DBI::errstr")', where `$class' is the driver class and `$method' is the name of the method that failed. E.g., DBD::Oracle::db prepare failed: ... error text here ... If you turn `RaiseError' on then you'd normally turn `PrintError' off. If `PrintError' is also on, then the `PrintError' is done first (naturally).
        Happy coding :)

        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)