Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Sqlite DBI $sth->rows

by digger (Friar)
on Oct 22, 2003 at 15:53 UTC ( [id://301256]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Sqlite DBI $sth->rows
in thread Sqlite DBI $sth->rows

Super Search to the rescue - again.

I had trouble with this issue, and found a decent thread here SELECT COUNT and DBI: rows that hashed out the issue pretty well with many experienced monks chiming in.

It looks like the most portable way of doing this is to SELECT COUNT.... before doing the actual SELECT.
You can also wrap your $sth->fetchrows in a while statement, which is ok if you just want to do nothing when no results are returned. It won't allow you to proactively deal with a situation where 0 records are returned, ie let a user know that their query returned no results.

hth,
digger

Replies are listed 'Best First'.
Re: Re: Sqlite DBI $sth->rows
by dragonchild (Archbishop) on Oct 22, 2003 at 18:38 UTC
    I'm not quite sure what you mean by "proactively", but this deals with the case of no rows found ...
    my $dbh = DBI->connect($conn_string, $user, $password, { RaiseError => 1, }) || die $DBI::errstr; my $sth = $dbh->prepare(<<__END_SQL__); SELECT stuff FROM some_place WHERE some_condition = 1 __END_SQL__ $sth->execute; my $num_rows = 0; while (my @x = $sth->fetchrow) { $num_rows++; do_something_with(@x); } die "No rows found\n" unless $num_rows; print "Done\n";

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Sqlite DBI $sth->rows
by b10m (Vicar) on Oct 22, 2003 at 17:30 UTC

    I fully agree. I had the same problems and scanned the web quite extensively. The SELECT COUNT method seems to work best (well, at least for me :) I'd prefer it over fetching all rows and counting them in a while loop, since that seems like a waste of resources to me (especially on really large data sets), and also over the $sth->rows, 'cause that doesn't seem to be reliable on SELECT queries.

    --
    B10m
    Yes, that is one long line ;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found