?
Solved

All tables in database using DBI

Posted on 2003-11-20
4
Medium Priority
?
373 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:TheTyrant
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 9789697
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
 
LVL 1

Author Comment

by:TheTyrant
ID: 9790077
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
 
LVL 18

Accepted Solution

by:
kandura earned 500 total points
ID: 9790131
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
 
LVL 1

Author Comment

by:TheTyrant
ID: 9790399
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question