Hi Perlmonks,

I am new at the Perl world and this is my first post and script

The below script generates .csv files corresponding to each executed query using DBI but, I am not happy with the code redundancy.

Had to search for some examples but didn't find anything. I would appreciate an example using a hash to understand how it works

Is there any nice way to do it?

Thanks in advance

# Create multiples csv file corresponding to each sql query my $csvfile1 = "csvfile1.csv"; my $csvfile2 = "csvfile2.csv"; my $csvfile3 = "csvfile3.csv"; # DBI CONNECTION my($dbh) = DBI->connect("dbi:Ingres:$dbname","$user","") or die "Could not connect to database $dbname\n"; #tb1 select my ($sth) = $dbh->prepare(SELECT * FROM tb1) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; #create csvfile1 according to tb1 select open my $fh, ">raw", $csvfile1 or die "$csvfile1: $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die "$csvfile1: $!"; $sth->finish(); #tb2 select my ($sth) = $dbh->prepare(SELECT * FROM tb2) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; #create csvfile2 according to tb2 select open my $fh, ">raw", $csvfile2 or die "$csvfile2: $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die "$csvfile2: $!"; $sth->finish(); #tb3 select my ($sth) = $dbh->prepare(SELECT * FROM tb3) or die "Prepare failed: $DBI::errstr\n"; $sth->execute() or die "Prepare failed: $DBI::errstr\n"; #create csvfile3 according to tb3 select open my $fh, ">raw", $csvfile3 or die "$csvfile3: $!"; $fh->print (join(",", @{$sth->{NAME}}), "\n"); #show header while (my @row = $sth->fetchrow_array()) { $fh->print (join(",", @row), "\n"); }; close $fh or die "$csvfile3: $!"; $sth->finish(); $dbh->disconnect();

In reply to 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":



  • 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.