omega_monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; use DBI; my $dsn = 'TEST'; # open the database handle using the ODBC driver to the TEST dsn my $dbh = DBI->connect("DBI:ODBC:$dsn") or die "Could not connect to t +he database: " . DBI->errstr; # get the table info from the dbase handle my $sth = $dbh->table_info("","","","TABLE") or die "Could not get the + table info: " . DBI->errstr; my @tables; # iterate through the rows of tables, and push the names of the tables + to an array(@tables) while (my ($catalog, $schema, $tab) = $sth->fetchrow_array()) { push(@ +tables,$tab); } # iterate through the tables names, and print the table name, the colu +mns , and the type/size of that column, etc foreach my $table (@tables) { print "create table $table (\n "; $sth = $dbh->column_info("", "", my $t=$table, ""); while ( my ( $cat, $schem, $tn, $cn, $dt, $tyn, $cs, $bl, $dd, $npr +, $nul) = $sth->fetchrow_array()) { print "\t$cn "; if (($tyn eq 'DATETIME') || ($tyn eq 'DOUBLE')) { print "$tyn"; } else { print "$tyn($cs)"; } print " not null" if ($nul == 0); # need to put a condition on adding the comma to not add the com +ma on the last loop. print ",\n"; } print ');'."\n\n"; } # disconnect the database handle $dbh->disconnect();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Don't print on the last loop
by Fletch (Bishop) on Jun 13, 2005 at 14:11 UTC | |
|
Re: Don't print on the last loop
by jdhedden (Deacon) on Jun 13, 2005 at 14:13 UTC | |
by calin (Deacon) on Jun 13, 2005 at 20:20 UTC | |
by omega_monk (Scribe) on Jun 13, 2005 at 14:30 UTC | |
|
Re: Don't print on the last loop
by jeffa (Bishop) on Jun 13, 2005 at 14:14 UTC | |
|
Re: Don't print on the last loop
by mrborisguy (Hermit) on Jun 13, 2005 at 14:15 UTC | |
|
Re: Don't print on the last loop
by merlyn (Sage) on Jun 13, 2005 at 18:35 UTC | |
by omega_monk (Scribe) on Jun 14, 2005 at 13:40 UTC | |
|
Re: Don't print on the last loop
by punkish (Priest) on Jun 13, 2005 at 14:48 UTC | |
|
Re: Don't print on the last loop
by tphyahoo (Vicar) on Jun 13, 2005 at 14:23 UTC |