# Accepts a database handle, a table name, and a hashref # of field names and values. Returns on success, otherwise # dies. sub addData { my ( $dbh, $table, $args ) = @_; # Get a comma separated list of the field names .. my $fields = join(',', keys %$args); # Get the values into an array .. my @values = values %$args; # Get a comma separated list of '?', one for each value .. my $placeholderList = join(',',('?') x @values); # Build the entire command .. my $cmd = "INSERT INTO $table ($fields) VALUES ($placeholderList)"; # Prepare the command and execute it with the array of values. my $sth = $dbh->prepare($cmd); $sth->execute(@values) or die $sth->error; return 0; # Success! }