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);

In reply to Re: Data export into text file by Lhamo Latso
in thread Data export into text file by b310

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.