Actually, you should be able to just use:
#!/usr/local/bin/perl -w
use strict;
use DBI;
use DBD::Pg;
my ($dbh, @tables);
my $user = "username";
my $password = "";
my $db_name="database";
my $driver="Pg";
my $db_host="localhost";
my $dsn="DBI:$driver:dbname=$db_name;host=$db_host";
$dbh=DBI->connect($dsn,$user,$password) or die "Can't connect: $DBI::
+errstr";
@tables = $dbh->tables;
for my $thisTable (@tables) {
my $sth=$dbh->prepare("select * from $thisTable where 1 = 0");
$sth->execute();
my $columnRef=$sth->{NAME_lc};
for my $thisColumn (@$columnRef) {
print "Table: $thisTable, column is: $thisColumn\n";
# Do whatever else you want with the columns.
}
1;
}
$dbh->disconnect();
-
@tables = grep { s/.*\.//o } @tables;
Would probably complain with use strict about cat’ing to an undef…. Right?
I guess that is why you used evals though.
And $dbh->tables with DBD:Pg will just return an array of table names anyway.
Of course, as the
DBI perldoc states: “Warning: This method is experimental and may change.”
So, although I agree with
quikwit that you should try to write things as portable as possibly… to some extent you will not be able to (tables() returning different things with CSV vs. Pg.) unless you have all possibly platforms to test/write on.
I did not know that SHOW was not in the SQL92 specs. either. ++
quikwit for pointing that out. I have been using SHOW TABLES myself.
If you want to access fields in the columns as DESCRIBE may have done, you should check out my question on feeding fields into an array:
Just another DBI question.
Ciao, -xtype
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.