I am trying to pull the schema out of an access database, in order to create the same schema in a mysql database. I am having one problem. I am adding a comma(and newline) to the end of each line that is output in the while loop of the column_info db connect. I need to not add the comma on the last iteration of that loop. I have looked everywhere, but I am unable to find the answer. Also if there is a "better" way that I could have done this, please edify me. Thanks.
#!/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();