All tables in database using DBI

Is there a way to get a list of all the tables withing a certain database?
for example, if i were to connect to a database named 'aaron' could i get a list of all the tables within 'aaron'?

thanks
LVL 1
TheTyrantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kanduraCommented:
have a look at the $dbh->table_info method in the DBI documentation. It should provide you with all the information you need.

Specific databases also have specific ways to get the table names. MySql has 'SHOW TABLES' for instance, and "SELECT NAME FROM sysobjects where TYPE='u'" works on MS SQLServer.

0
TheTyrantAuthor Commented:
could you give me an example of how to use table_info properly? still fairly new with perl & the dbi package :/

im interfacing with multiple database server (so far only MS SQL, and MS Access, plus more later) and need to put a table selection list into a Tk app.
0
kanduraCommented:
Actually, I just thought I should do that, and have been looking into it.

I have noticed that the DBI interface to that method has changed somewhere between versions 1.14 and 1.37. (these coincide with ActivePerl5.6.1 and ActivePerl5.8.0 on my machine).

In the old version you should do this (assuming $dbh is your database handle):

$sth = $dbh->table_info();
while(my @r = $sth->fetchrow_array)
{
    print join("\t", @r), $/;
}

You will see that column 3 is the table name, and column 5 the type.

in the new version you can do more, ie. filter on catalogs and schema's.
I tried it on a small MySql database:
$sth = $dbh->table_info('%','%','TABLE','');
while(my $r = $sth->fetchrow_hashref)
{
      print map { "$_\t$r->{$_}$/" } sort keys %$r;
}

This prints out the fieldname and value on a line.
an example record for my database is
REMARKS      
TABLE_CAT      
TABLE_NAME      tblTest
TABLE_SCHEM      
TABLE_TYPE      TABLE


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TheTyrantAuthor Commented:
thanks :)

the first example worked perfectly - 174 different tables in ms access db

the second example gave me this:
DBD::ODBC::db table_info failed: [Microsoft][ODBC Microsoft Access Driver]Optional feature not implemented  (SQL-HYC00)
(DBD: st_tables/SQLTables err=-1) at C:\Cacoon\examples\odbc\tbllst.pl line 12.
Can't call method "fetchrow_hashref" on an undefined value at C:\Cacoon\examples\odbc\tbllst.pl line 13.

either way i should be able to figure it out from here now.
thanks a lot :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.