in reply to Re: prepare statement within DBI
in thread prepare statement within DBI
For most of the duplication you avoid with your fetch_count function, DBI has a built-in way of avoiding it: whenever you've got a query on which ->fetch_... is only invoked once, you can skip the separate ->prepare and ->execute by using a $db->select_... method.
So the 4 active lines in your function become just 1:
sub fetch_count { my ($db, $table) = @_; $db->selectrow_array("SELECT COUNT(*) FROM $table"); }
It probably isn't worth writing a function for that, since there isn't that much duplication without it:
my $row1 = $dbh->selectrow_array('SELECT COUNT(*) FROM table_one'); my $row2 = $dbh->selectrow_array('SELECT COUNT(*) FROM table_two');
But you could always put it in some sort of loop:
my ($row1, $row2) = map { $dbh->selectrow_array("SELECT COUNT(*) FROM $_") } qw<table_one table_two>;
(By the way, I think your ->fetch_arrayref is a typo.)
Smylers
|
|---|