note
Tux
<p><c>f_ext</c> was added in [mod://DBI]-1.608 and [mod://DBD::CSV]-0.25, so that should work. <c>f_encoding</c> however was added in [mod://DBI]-1.611, so that will not.</p>
<p>You did not mention the version of [mod://SQL::Statement], which is the SQL parser for [mod://DBD::CSV] and essential for almost any statements more complicated than <c>select foo from bar;</c>. You use <c>LIKE</c>.</p>
<p>If you want to use literal key names, you can almost never rely on its casing the way you do. You should tell [mod://DBI] explicitly to return your key names in upper case or lower case when using <c>fetchrow_hashref</c>. Better yet, do not rely on this at all</p>
<p>The case insensitiveness for table names should work as of [mod://DBD::CSV]-0.25, so you should be safe there too.</p>
<p>Use a CSV module to output your data!</p>
<code>
use warnings;
use strict;
use DBI;
use Text::CSV_XS;
# Create connection string to database point.csv
my $dbh = DBI->connect ("dbi:CSV:", undef, undef, {
# CSV specific attributes
f_ext => ".csv/r",
f_encoding => "utf-8",
# DBI attributes
RaiseError => 1,
PrintError => 1,
ChopBlanks => 1,
ShowErrorStatement => 1,
FetchHashKeyName => "NAME_uc", # You want uc (I prefer lc)
});
my $sth = $dbh->prepare ("select * from point where ID_DEVTYP like 'INTELI%' AND ID_POINT like 'AUTO%'");
$sth->execute;
my @columns = @{$sth->{NAME_uc}};
# Create AFS CSV with Columns
my $csv = Text::CSV_XS->new ({ binary => 1, eol => "\r\n" });
open my $fh, ">", "AFS.csv" or die "AFS.csv: $!";
$csv->print ($fh, \@columns);
# Cycle through SQL results on ROW basis and print to AFS CSV file
while (my $row = $sth->fetchrow_hashref) {
$csv->print ($fh, [ @{$row}{@columns} ]);
}
# Close file
close $fh;
</code>
<p>With [mod://Text::CSV_XS]-1.07 you can even simplify that to:</p>
<code>
use Text::CSV_XS qw( csv );
:
:
my $sth = $dbh->prepare ("select * from point where ID_DEVTYP like 'INTELI%' AND ID_POINT like 'AUTO%'");
$sth->execute;
csv (out => "AFS.csv", in => sub { $sth->fetchrow_hashref });
</code>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-200365">
<br />
Enjoy, Have FUN! H.Merijn
</div></div>
1089635
1090189