1nickt has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Select DB data into CSV file
by Tux (Canon) on Dec 20, 2018 at 13:02 UTC | |
by 1nickt (Canon) on Dec 20, 2018 at 13:06 UTC | |
by Tux (Canon) on Dec 20, 2018 at 13:14 UTC | |
by 1nickt (Canon) on Dec 20, 2018 at 13:24 UTC | |
|
Re: Select DB data into CSV file
by perlancar (Hermit) on Dec 20, 2018 at 16:59 UTC | |
by 1nickt (Canon) on Dec 20, 2018 at 17:36 UTC | |
|
Re: Select DB data into CSV file
by Your Mother (Archbishop) on Dec 20, 2018 at 21:27 UTC | |
by Tux (Canon) on Dec 21, 2018 at 07:24 UTC | |
|
Re: Select DB data into CSV file
by kschwab (Vicar) on Dec 20, 2018 at 15:56 UTC | |
by afoken (Chancellor) on Dec 20, 2018 at 16:52 UTC | |
by kschwab (Vicar) on Dec 20, 2018 at 16:58 UTC |