in reply to DBI - MySQL vs. PgSQL
DBI does allows the user to get a list of tables through the $dbh->tables command.
The way I get the column names is just run a bogus search: "select * from $table where 1 = 0". This should run very quickly and get me the information I want.
Here's a sample program:
A few things to note:#!/usr/bin/perl -w use strict; use DBI; my ($driver, $dbh, @tables); $driver="DBI:CSV:f_dir=Tables"; $dbh=DBI->connect($driver) or die "Can't connect: $DBI::errstr"; eval { @tables = $dbh->tables; @tables = grep { s/.*\.//o } @tables; } or die "Couldn't retrieve list of tables: $DBI::errstr\n"; for my $thisTable (@tables) { eval { 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; } or die "Couldn't read table $_: $DBI::errstr\n"; } $dbh->disconnect();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: DBI - MySQL vs. PgSQL
by xtype (Deacon) on Jan 19, 2002 at 03:05 UTC |