Solved

All tables in database using DBI

Posted on 2003-11-20
4
328 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
  • 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 125 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

791 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