パルカワ2

最近はFlutterをやっています

SQLからCSV

FIELDS TERMINATED BYうんたら毎回ググってる。

use 5.10.1;
use strict;
use warnings;

use DBI;
use Encode;
use Text::CSV;

my $sql = shift @ARGV;
my $dbh = DBI->connect(...);
my $sth = $dbh->prepare($sql);
my $csv = Text::CSV->new;

$sth->execute();

$csv->combine(@{$sth->{NAME}});
say encode_utf8 $csv->string;

while (my $row = $sth->fetchrow_arrayref) {
    $csv->combine(@$row);
    say encode_utf8 $csv->string;
}

# perl $0 "select * from players;" > players.csv

便利