Using RDBMS-specific dumps like MySQL's "SELECT INTO OUTFILE" is probably faster. But in case your RDBMS doesn't have one or you want something that is portable across all DBI-accessible RDBMSs, here's an example. You can substitute any DBD for XBase and use any SELECT statement that is supported by that DBD. Under the hood, SQL::Statement will use that DBD to read row by row from the source and use Text::CSV_XS to write row by row to the target.

To use different separators, delimters, or escapes, use DBD::CSV's csv_tables, for example for a so-called "Tab Delimted" file with *nix line endings:

$dbhC->{csv_tables}->{outTable} = { file => 'foo/bar.csv', sep_char => "\t", eol => '\012', };
If you omit the csv_tables defintion, then the table name will be used for the filename and the separator will be a comma, the delimiter a quote mark and the line ending windows-style '\015\012'.

#!/usr/bin/perl use warnings; use strict; use DBI; my $dbhX = DBI->connect('dbi:XBase(RaiseError=1):'); my $dbhC = DBI->connect('dbi:CSV(RaiseError=1):'); my $select = $dbhX->prepare("SELECT * FROM inTable"); $select->execute(); $dbhC->do("CREATE TABLE outTable AS IMPORT(?)",{},$select); __END__

In reply to Dumping from any RDBMS to CSV by jZed

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.