Skip to content

Instantly share code, notes, and snippets.

@sirhc
Created March 2, 2011 00:05
Show Gist options
  • Save sirhc/850178 to your computer and use it in GitHub Desktop.
Save sirhc/850178 to your computer and use it in GitHub Desktop.
Use a field/value hash to automatically generate placeholders for DBI
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