Tony said:
[snip]
Sorry for the incorrect line wrapping.
#!/usr/bin/perl
use strict;
use warnings;
use DBI();
my $DBserver= "server";
my $DBname= "dbname";
my $DBuser= "user";
my $DBpassword= "pwd";
my $dbh = DBI->connect("DBI:mysql:database=$DBname;host=$DBserver",
"$DBuser", "$DBpassword",{'RaiseError' => 1});
my %fieldnames;
my $sth = $dbh->prepare("show tables");
$sth->execute();
while (my $refv = $sth->fetchrow_hashref()) {
my $sth2 = $dbh->prepare("describe ".$refv->{"Tables_in_$DBname"});
$sth2->execute();
while (my $refv2 = $sth2->fetchrow_hashref()) {
$fieldnames{$refv->{"Tables_in_$DBname"}.".".$refv2->{Field}} =
$refv->{"Tables_in_$DBname"}.".".$refv2->{Field}; }
$sth2->finish(); }
$sth->finish();
$dbh->disconnect();
foreach my $columns (keys %fieldnames) {
print "$columns\n"; }
exit 0;
D:\>perl t2.pl
testtie.produce_id
testtie.quantity
testtie.price
test.testid
pdata.pfrozen
testtie.description
test.testdata
pdata.pvalue
pdata.pkey
or
use strict;
use warnings;
use DBI();
my $DBserver= "server";
my $DBname= "dbname";
my $DBuser= "user";
my $DBpassword= "pwd";
my $dbh = DBI->connect("DBI:mysql:database=$DBname;host=$DBserver",
"$DBuser", "$DBpassword",{'RaiseError' => 1});
my %HOA;
my $sth = $dbh->prepare("show tables");
$sth->execute();
while (my $refv = $sth->fetchrow_hashref()) {
$HOA{$refv->{"Tables_in_$DBname"}} = [];
my $sth2 = $dbh->prepare("describe ".$refv->{"Tables_in_$DBname"});
$sth2->execute();
while (my $refv2 = $sth2->fetchrow_hashref()) {
push @{$HOA{$refv->{"Tables_in_$DBname"}}},$refv2->{Field}; }
$sth2->finish(); }
$sth->finish();
$dbh->disconnect();
print "Database $DBname\n";
foreach my $table (keys %HOA) {
print "Table $table\n";
print "Fields ",join(",",@{$HOA{$table}}),"\n"; }
exit 0;
D:\>perl test.pl
Database test
Table testtie
Fields produce_id,price,quantity,description
Table pdata
Fields pkey,pvalue,pfrozen
Table test
Fields testid,testdata
There are other (simple) solutions, though, I reckon...
You would also want to read the table_info and column_info section of the
DBI documentation which will answer your questions.
HTH,
Zoltan