Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Multiple queries on DBI corresponding to multiple csv files?

by poj (Abbot)
on Feb 18, 2019 at 11:46 UTC ( #1230090=note: print w/replies, xml ) Need Help??


in reply to Multiple queries on DBI corresponding to multiple csv files?

There is an example in Text::CSV_XS here

#!/usr/bin/perl use strict; use warnings; use DBI; use Text::CSV_XS; # Create multiples csv file corresponding to each sql query my @query = ( ['SELECT * FROM account','accounts.csv'], ['SELECT * FROM facts','facts.csv'], ['SELECT * FROM mytable','mytable.csv'], ); # DBI CONNECTION my $dbh = get_dbh(); for (@query){ run_query(@$_); } sub run_query { my ($sql,$csvfile) = @_; print "Running $sql\n"; open my $fh, '>', $csvfile or die "Could not open $csvfile: $!"; my $csv = Text::CSV_XS->new ({ binary => 1, eol => "\n" }); my $sth = $dbh->prepare ($sql); $sth->execute; $csv->print($fh, $sth->{NAME_lc}); my $count = 1; while (my $row = $sth->fetchrow_arrayref) { $csv->print($fh, $row); ++$count; } close $fh; print "$count lines dumped to $csvfile\n"; } sub get_dbh { my $dbname = ''; my $user = '****'; my $pw = ''; my $dsn = "dbi:Ingress:$dbname"; my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError=>1, AutoCommit=>1 } ); return $dbh; }
poj

Replies are listed 'Best First'.
Re^2: Multiple queries on DBI corresponding to multiple csv files?
by jtech (Sexton) on Feb 18, 2019 at 12:14 UTC
    Nice! But, sadly I don't have permission to install Perl modules on the server, I am afraid that cannot use Text::CSV_XS :(

      Ok, then maybe use Text::CSV_PP or are you sure you will never have a table with fields containing commas ?

      poj
        Right, I will check this out :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2019-03-23 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (114 votes). Check out past polls.

    Notices?