Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: prepare statement within DBI

by grinder (Bishop)
on Aug 25, 2004 at 15:42 UTC ( [id://385710]=note: print w/replies, xml ) Need Help??


in reply to prepare statement within DBI

I could combine the two sql statements into one statement

Well, you could with a union:

select 't1', count(*) from t1 union select 't2', count(*) from t2

... but you must add something to each select (here, the name of the table as a string) to what is returned in order to guarantee that you can determine which count(*) corresponds to which table. You never know which order they'll come back in. But now you have just about as much make-work code to pick the apart the result set and get what you want.

My advice would be to encapsulate it in a sub and use cached prepares.

my $t1_count = table_row_count( $dbh, 'table1' ); my $t2_count = table_row_count( $dbh, 'table2' ); sub table_row_count { my $db = shift; my $table = shift; my $sth = $db->prepare_cached( "select count(*) from $table" ); $sth->execute(); return $sth->fetchrow_array; }

Error checking is left as an exercise to the reader.

- another intruder with the mooring of the heat of the Perl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found