Gossamer Forum
Home : General : Perl Programming :

Using Perl to count MySQL tables

Quote Reply
Using Perl to count MySQL tables
Hi all,

I'm trying to count the amount of tables that I have in a database. So I was wondering if this is possible or not.

I want to do this because my script sort of depends on the amount of tables in a database. Now I have it hardcoded, but I do not really like that. So I hope anybody knows this.

Thank you in advance,

Jorn.
Quote Reply
Re: [Jorn] Using Perl to count MySQL tables In reply to
Hi,

I'm not sure if you can do this. Counting "rows" is easy enough.

For tables, you'd probably need something like;

Code:
# connect here...

my @tables = qw/table1 table2 table3 table4/;
my @bad;
foreach my $table (@tables) {
my $sql = qq{SELECT * FROM ? };
$sth->prepare($sql);
$sth->execute($table) || push @bad, $table;
}

if ($#bad > 0) {
print "Some table's couldn't be found. They are as follows;\n<br><br><ul>";
print join("<li>",@bad);
print "</ul>";
}

# close connection

Hopefully something like that will work :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Using Perl to count MySQL tables In reply to
Hi Andy,

Thank you for your reply. Unfortunately, your script is of little use to me since you still need to hardcode the tables into an array. I guess I'll have to think about something else then.

Thanks,

Jorn.
Quote Reply
Re: [Jorn] Using Perl to count MySQL tables In reply to
I think what you want is just "show tables".

Code:
my $tables = [];

my $sth = $dbh->prepare("show tables");
$sth->execute();

while (my $table = $sth->fetchrow()) {
push @$tables, $table;
}

my $count = scalar @$tables;

Philip
------------------
Limecat is not pleased.

Last edited by:

fuzzy logic: Apr 11, 2005, 8:55 AM