Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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

by huck (Parson)
on Feb 18, 2019 at 11:19 UTC ( #1230086=note: print w/replies, xml ) Need Help??

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

I'll leave it to others to comment about using other modules to write csv files so proper quoting is performed and just discuss your imediate question

This is untested, demonstration code

use strict; use warnings; use DBI; my @clumps=( {table=>'tb1',file=>"csvfile1.csv"}, {table=>'tb2',file=>"csvfile2.csv"}, {table=>'tb3',file=>"csvfile3.csv"} ); my $dbname='a'; my $user='b'; # DBI CONNECTION my($dbh) = DBI->connect("dbi:Ingres:$dbname","$user","") or die "Could not connect to database $dbname\n"; for my $clump (@clumps){ my ($sth) = $dbh->prepare('SELECT * FROM '.$clump->{table}) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; open my $fh, ">raw", $clump->{file} or die $clump->{file}. ": $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die $clump->{file}.": $!"; $sth->finish(); } $dbh->disconnect();
Notice how i used an array of hashes to contain the table name and outfile file, then cycled thru it to do each table/outfile file pair

Edit: Hippo was right "You've set @clumps as an arrayref but then treat as an array. Typo?"

he saw it as

my @clumps=[ {table=>'tb1',file=>"csvfile1.csv"}, {table=>'tb2',file=>"csvfile2.csv"}, {table=>'tb3',file=>"csvfile3.csv"} ];

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2020-07-10 13:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found