use DBI qw( );
my $dbh = DBI->connect($dsn, $user, $passwd, {
RaiseError => 1,
PrintError => 0,
PrintWarn => 1,
AutoCommit => 1,
FetchHashKeyName => 'NAME_lc',
});
my $sth = $dbh->prepare('
SELECT *
FROM Table
ORDER BY ...
');
$sth->execute();
while (my $row = $sth->fetch()) {
my $fn = join('-', @{$row}[0,1,2]);
my $qfn = "c:\\database\\first_past\\" . $fn;
open(my $fh, '>', $qfn) or die $!;
print($fh ...);
}
DBI , open
,print
对于您的后续问题:
$sth->execute();
while (my $row = $sth->fetch()) {
my $fn = join('-', @{$row}[0,1,2]);
my $qfn = "c:\\database\\first_past\\" . $fn;
open(my $fh, '>>', $qfn) or die $!;
print($fh ...);
}
或者
my $last_fn;
my $fh;
$sth->execute();
while (my $row = $sth->fetch()) {
my $fn = join('-', @{$row}[0,1,2]);
if (!defined($last_fn) || $last_fn ne $fn) {
$last_fn = $fn;
my $qfn = "c:\\database\\first_past\\" . $fn;
open($fh, '>', $qfn) or die $!;
}
print($fh ...);
}