in reply to Re: Sqlite DBI $sth->rows
in thread Sqlite DBI $sth->rows
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Sqlite DBI $sth->rows
by hmerrill (Friar) on Oct 22, 2003 at 15:34 UTC | |
As the perldocs suggest, here's an example of doing a SELECT COUNT to get the number of rows found: or, you could fetch all the rows, one-by-one, and count them as you go, like: Of course, I haven't done anything with error trapping the DBI statements here, but you definitely should. HTH. | [reply] [d/l] [select] |
by bart (Canon) on Oct 24, 2003 at 00:35 UTC | |
Note that this is not complete without a finish() call: You need to use finish if you don't repeat calling fetchrow_*, or equivalent, until it returns undef. So you need it in your second example, too. If you omit it, you'll get a warning from DBI, or you should. (Maybe this depends on the database driver?) | [reply] [d/l] [select] |
|
Re: Sqlite DBI $sth->rows
by digger (Friar) on Oct 22, 2003 at 15:53 UTC | |
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 | [reply] |
by dragonchild (Archbishop) on Oct 22, 2003 at 18:38 UTC | |
------
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. | [reply] [d/l] |
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.
-- | [reply] |