Created
March 2, 2011 00:05
-
-
Save sirhc/850178 to your computer and use it in GitHub Desktop.
Use a field/value hash to automatically generate placeholders for DBI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my %value_for = ( | |
field1 => 'value1', | |
field2 => 'value2', | |
field3 => 'value3', | |
# ... | |
); | |
my $sth = $dbh->prepare(<<"SQL"); | |
INSERT INTO table (@{[ join ',', keys %value_for ]}) | |
VALUES (@{[ join ',', ('?') x scalar keys %value_for ]}) | |
SQL | |
# Alternate for VALUES: | |
# join ',', map { '?' } keys %value_for | |
$sth->execute( values %value_for ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment