in reply to DBI: Better way to find number of rows in a table?
Indeed there isn't any way to dynamically set table names in DBI, or indeed in most databases. The reason why comes down to what a ->prepare() is actually doing. (Note: this is somewhat platform specific, so YMMV here). The prepare tells the database to get the SQL ready and parsed so that it can be executed with a variety of parameters in the where clause. This is an efficiency issue for the DB, and there is no way to dynamically avoid it.
So, what can you do?
sub make_sql($) { return "SELECT COUNT(*) FROM $_[0]"; } if ($dbh->selectrow_array(make_sql($tablename))) { # do stuff }
A final note: something seems buggy to me in using the selectrow_array method directly in the if condition, but I can't put my finger on what it is. You might want to break it into a separate variable $count = $dbh->selectrow_array(make_sql($tablename));
Hth!Update: I just realized that my response is nearly identical to hmerril's just above....
|
|---|