Solved

All tables in database using DBI

Posted on 2003-11-20
4
301 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now