Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.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 snippet below does what I have described, from processing each table in the database.
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.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"; }
Cheers, Steve
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Obtaining field names only
by jZed (Prior) on Feb 17, 2005 at 19:31 UTC | |
|
Re: Obtaining field names only
by Animator (Hermit) on Feb 17, 2005 at 18:47 UTC | |
|
Re: Obtaining field names only
by RazorbladeBidet (Friar) on Feb 17, 2005 at 18:46 UTC |