Hello all, I am wondering about the most elegant, performant, and robust way to select data from a database and output a delimited file (e.g. CSV). We use MySQL so manually running SELECT ... INTO OUTFILE ... is always possible, and I have colleagues who think that's fine since "we know there will *never* be a tab in the data" ... Personally I hate shelling out to begin with and would always rather use Perl, and I *never* trust that there will *never* be an occurrence of something that can *never* happen. We use DBIx::Class so in application code we are always querying on a DBIC ResultSet.

Naturally I can select data into an AoA with selectall_arrayref() and just use csv() from Text::CSV_XS, but I am wondering if there isn't an interface. I see that perlancar has released DBIx::CSV which provides methods like $dbh->selectall_csv( $sql ), and I have tried it, and it worked. But I have two reservations about it: (1) It extends DBI rather than DBIx::Class, so would require me to "get my hands dirty" selecting the data with DBI, and (2) while the author has created a panoply of interdependent modules, I am always leery of one-person ecosystems, and in this case the backend is another of his/her modules, Text::Table::CSV. I am just solidly in the Text::CSV_XS camp at this point and don't plan to introduce another CSV-handling library.

(Is DBIx::Class extensible, as in, add a keyword something like $rs->search({...})->csv() ?

Thanks for your wisdom.

Edit: clairfy proposed DBI solution


The way forward always starts with a minimal test.

In reply to Select DB data into CSV file by 1nickt

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.