Solved

All tables in database using DBI

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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