my @keys = sort keys %cols; my $keystring = join ', ', @keys; my $valstring = join ', ', ('?') x scalar @keys; my $statement = qq{ INSERT INTO service_metrics ($keystring) VALUES ($valuestring) }; my @values = map { $cols{$_} } @keys; my $sth = $dbh->prepare($statement); if (! $sth) { die(sprintf('[FATAL] Could not prepare statement: %s\n\n + ERRSTR: %s\n', $db->errstr())); } $sth->execute(@values); if (! $sth) { die(sprintf('[FATAL] Could not execute statement: %s\n\n + ERRSTR: %s\n', $db->errstr())); } $sth->finish; $db->disconnect();