in reply to Data export into text file
Here is some code I used once to create CSV output from a list of tables. I had to add character counting so that output files were no greater that 1GB. If you remove that IF stmt, the loop will be very simple. $sth2 is simply "select * from some_table"
But you will need the DBD and DBI for this code to be useful.
my @row;
my $sth2 = $dbh->prepare( $statement );
### Execute the statement in the database
$sth2->execute
or die "Can't execute SQL statement: $DBI::errstr\n";
my $file_len = 0;
### Retrieve the returned rows of data
while ( @row = $sth2->fetchrow_array() ) {
if ($file_len + 1 + length( $row[0] ) > 1073741824) {# break file at 1GB
$file_len = 0;
close (FINAL);
$fn++;
$filename = sprintf("%s_%03d.csv", ${table_name}, ${fn});
$pathname = "$list_path/$filename";
open (FINAL, ">$pathname") || die "Can't open $pathname for output";
}
$file_len += length( $row[0] );
$file_len++;
$rowctr++;
print FINAL "@row\n";
}
warn "Data fetching terminated early by error: $DBI::errstr\n"
if $DBI::err;
close (FINAL);
|
|---|