Wise Ones,
I am trying to print out the field names of each table in a mysql db and then the values.

I can get the values and the field names but I can't seem to get them in the format I want.
I basically want to output the contents of a database with several tables in CSV format, like so:

field1, field2, f3, f4, f5 Value1, value2, v3, v4, v5 Value1, value2, v3, v4, v5 field1, field2, f3, f4, f5, f6, f7 Value1, value2, v3, v4, v5, v6, v7 Value1, value2, v3, v4, v5, v6, v7
The problem I keep getting is that i'm printing the field names and values for each row in the table. Can't just seem to print the field names then each row below.

The snippet below does what I have described, from processing each table in the database.

if ($format eq "CSV") { while ($row = $sth->fetchrow_hashref) { while( my ($k, $v) = each %{$row} ) { print $k; } foreach $col (keys %{$row}) { print $$row{$col} . ","; } print "\n"; } print "\n"; }
Im sure there's a fairly easy answer to this but I cant seem to find it in the DBI tutorial here or through searching google.

Cheers, Steve


In reply to Obtaining field names only by Anonymous 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.