Question : Add Array headers to CSV

Hello, I am wondering what the proper code format would be to add the column headers from the array into the CSV file.  Wilcoxon was nice enough to provide the CSV script which works perfect and the code is listed below.  Thanks for your help!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
use strict;
use warnings;
use DBI;
# change $out_file to whatever path/name you want
my $out_file = 'output.csv';
open OUT, '>', $out_file or die "could not write $out_file: $!";
# lookup the DBD::Oracle docs for the correct connection string
my $dbh = DBI->connect(...);
# you need to change the query to what you want
my $sql = 'select col1, col2, col3 from table where x = y';
my $sth = $dbh->prepare($sql);
$sth->execute;
while (my $row = $sth->fetchrow_arrayref) {
    print OUT join(',', map { s{"}{""}g; "\"$_\""; } @$row), "\n";
}
$sth->finish;
$dbh->disconnect;

Answer : Add Array headers to CSV

The simplest way to get the column headers into the CSV (unless using wildcard columns in the sql select) would be to add a line after line 10:

print OUT "col1,col2,col3\n"; # whatever columns you select in the sql at line 10

To get the column names programmatically is more difficult (eg I can't remember off the top of my head and will have to look it up).
Random Solutions  
 
programming4us programming4us