Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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"} ];


In reply to Re: Multiple queries on DBI corresponding to multiple csv files? by huck
in thread Multiple queries on DBI corresponding to multiple csv files? by jtech

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-03-28 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found