in reply to Re: A Case with 5 Var's
in thread A Case with 5 Var's
I've no idea what kind of "qry" you're talking about. But let's assume you want a database query where the contents of the where clause are controlled by the existance of data items that are stored in a hash. The keys of the hash are the column names and the values are the values that are required.
In the past I've used code something like this:
my $sql = 'select col_x, col_x from a_table'; # You'll be setting this hash up from some kind of # input to your program. I'm using variables for # illustrative purposes. my %cols = ( col_x => $foo, col_y => $bar, col_z => $baz, ); my @where; my @vals; foreach (keys $cols) { if (defined $cols{$_}) { push @where, "$_ = ?"; push @vals, $cols{$_}; } } if (@where) { $sql .= ' where ' . join(' and ', @where); } my $sth = $dbh->prepare($sql); $sth->execute(@vals);
Update: Or use SQL::Abstract as Corion points out.
--
"The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: A Case with 5 Var's
by ultibuzz (Monk) on Jan 25, 2007 at 16:13 UTC | |
by pemungkah (Priest) on Jan 25, 2007 at 20:09 UTC |
In Section
Seekers of Perl Wisdom