Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: SQLite: how to get a number of rows from SELECT query

by derby (Abbot)
on Jul 20, 2021 at 11:18 UTC ( [id://11135213]=note: print w/replies, xml ) Need Help??


in reply to SQLite: how to get a number of rows from SELECT query

This is why my favorite DBI method is selectall_arrayref with the attribute Slice set

my $rows= $dbh->selectall_arrayref( "SELECT * FROM table", { Slice = +> {} } );

Sure it looks wonky and pretentious but this approach gives my an array reference of hash references where the hash key is the column name and the values are well, the data values. Now you can just use perl to get the size:

printf "Executed Statement - returned (%d) rows\n", scalar( @$rows ) +;

As a developer the concept of prepare, execute, and fetch is alluring and I can see its' value in certain situations but pragmatically, wrapping those steps into one is the common case with re-use of statement handle being the uncommon case (and for the future comments, I said uncommon - not rare). I am aware of the downsides to this approach such as the result set being too large and needing to be fetched in smaller batch sizes but I find a lot of the downsides to be outlier cases that are rarely hit (at least for my domain).

-derby

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found