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();

In reply to Don't print on the last loop by omega_monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.