Always best to use placeholders in your queries if possible
poj#!/usr/bin/perl use strict; use warnings; use DBI; use Text::CSV_XS; # DBI CONNECTION my $dbh = get_dbh(); my @id = @ARGV; unless (@id){ # no ids in ARGV my $sql = "SELECT DISTINCT id from mytable WHERE active = ?"; @id = map{ $_->[0] } $dbh->selectall_array($sql,undef,'no'); } # MAIN for my $id (@id){ my @query = ( ["SELECT id FROM mytable WHERE id=? AND salary >= 1","csvfile1 +_$id.csv"], ["SELECT name FROM mytable WHERE id=? AND salary >= 1","csvfile2 +_$id.csv"], ["SELECT salary FROM mytable WHERE id=? AND salary >= 1","csvfile3 +_$id.csv"], ["SELECT dept FROM mytable WHERE id=? AND salary >= 1","csvfile4 +_$id.csv"], ); for (@query){ run_query(@$_,$id); } } # SUBROUTINES sub run_query { my ($sql, $csvfile,$id) = @_; print "Running $sql for $id\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($id); $csv->print($fh, $sth->{NAME_lc}); my $count = 1; while (my $row = $sth->fetchrow_arrayref) { $csv->print($fh, $row); ++$count; } close $fh; print "ID: $id, $count lines dumped to $csvfile\n"; } sub get_dbh { my $dbname = "jtech"; my $user = "jtech"; my $dbh = DBI->connect("dbi:IngresII:$dbname",$user,'') or die "Could not connect to database $dbname\n"; return $dbh; }
In reply to Re^2: Multiple queries on DBI corresponding to multiple csv files?
by poj
in thread Multiple queries on DBI corresponding to multiple csv files?
by jtech
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |