in reply to OT - Searching databases effectively
Here's one way I constructed a dynamic query based on the values at hand. Probably not the best way, but it works well for my purposes.
# $insert_values = { ...hashref of keys and values... }; # # Break up the hash into separate pieces for columns # and values to use in the sql insert statement. # my @keylist; push @keylist, [ $_, $insert_values->{$_} ] for keys %$insert_values; my ( @columns, @values ); for ( @keylist ) { push @columns, $_->[0]; push @values, $_->[1]; } # # Construct our query based on the lists of columns and # their values. # my $sql = "INSERT INTO table_foo (\n" . join( ", ", @columns ) . "\n) VALUES (\n" . join( ", ", @values ) . "\n)\n";
--
"This alcoholism thing, I think it's just clever propaganda produced by people who want you to buy more bottled water." -- pedestrianwolf
|
|---|