my $dumper = Data::Dumper->new();
$dumper->Terse(1);
$dumper->Indent(0);
my $sth = $dbh->prepare( 'SELECT * FROM table' );
$sth->execute();
# open a file to track all ids
my $id_file_name = 'all-ids.txt';
open my $id_fh, "> $id_file_name"
or die "opening $id_file_name: $!"
# array position of primary key
my $id_ix = 0;
while ( my $cur = $sth->fetch() )
{
my $id = $cur->[$id_ix];
open my $fh, "> $id"
or { do warn "opening $id: $!"; next };
print $fh $dumper->($cur);
close $fh or warn "closing $id: $!";
print $id_fh "$id\n";
}
close $id_fh
or die "closing $id_file_name: $!";
$sth->finish();
####
# open a file to track all ids
my $id_file_name = 'all-ids.txt';
open my $id_fh, $id_file_name
or die "opening $id_file_name: $!"
my @ids = map { s/\s+$//; $_ } <$id_fh>;
close $id_fh or die "closing $id_file_name: $!";
my $n_cols = XXX; # how many columns in "table"?
my $places = join ',', ('?') x $n_cols;
my $ins = $dbh->prepare( "INSERT INTO table" .
" VALUES ( $places )" );
local $/ = undef;
foreach my $id ( @ids )
{
open my $fh, $id
or do { warn "opening $id: $!"; next };
my $str = <$fh>;
close $fh
or do { warn "closing $id: $!"; next };
my $aref = eval $str;
if ( !$aref || $@ )
{
warn "$id: eval failed (str='$str')";
next;
}
$ins->execute( @$aref );
}
####
my $subdir = sprintf '%03d', $id % 1000;
unless ( -d $subdir ) { mkdir $subdir, 0777; }
my $file = "$subdir/$id";