Hi 1nickt,

Thanks for the mention. I linked DBIx::CSV to Text::Table::CSV instead of Text::CSV_XS directly because DBIx::CSV is just a special case of DBIx::TextTableAny to output to various forms of text tables, which CSV is one of.

I wrote a short blog post on DBIx::CSV here and noted that you can also do something like:

use Text::CSV; my $csv = Text::CSV->new; ... $csv->print(\*STDOUT, $sth->{NAME}); while (my $row = $sth->fetchrow_arrayref) { $csv->say(\*STDOUT, $row); }

which doesn't require any additional module and not much extra code anyway. But since you do not use DBI directly, that's a bit of a problem as well.

I haven't looked into DBIx::Class to know if it's easily extensible to do what you want. But how about a patch-based solution like Patch::DBI::WriteCSV (use this if the link doesn't exist yet). As long as something like DBIx::Class calls one of DBI's fetchrow_{array,arrayref,hashref} or fetchall_{arrayref,hashref}, it will work. I've tested it with the simple sample database from the DBIx::Class documentation. This code:

Patch::DBI::WriteCSV->import; my @all_artists = $schema->resultset('Artist')->all; Patch::DBI::WriteCSV->unimport;
will produce something like:
artistid,name
1,Enya
2,"Mariah Carey"

If you don't want to depend on my module, you can just use the underlying code and integrate that to your own code base. As for the "one-person ecosystem" remark: there's a line between NIH syndrome and "not finding what I want/need on CPAN" and I can say that I am still on the second camp. But I can't help if what I want/need is not on CPAN and thus write a module for it :-) I do use other people's modules. Of my 1512 distributions on CPAN, I depend on 610 modules written by others.

% lcpan dists --author PERLANCAR | xargs lcpan deps | grep -v PERLANCAR | wc -l

In reply to Re: Select DB data into CSV file by perlancar
in thread 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.