Perhaps dhoss might want to consider (sorry, this is untested):
sub insert { my $self = shift; my ($table, $columns, $values) = @_; my $sql = sprintf("insert into %s (%s) values (%s)", $table, join(',', @$columns), join(',' map { '?' } @$values); my $sth = $dbh->prepare($sql) or die $dbh->errstr; $sth->execute(@$values) or die $sth->errstr; }
You'd call it as
It will give you a reasonably useful error message (at least in MySQL) if the table structure stops matching the column names in your code. Plus, using ? placeholders in the prepared statement, and passing the values to execute will handle all the quoting issues for you.$obj->insert($tablename, \@column_names, \@values)
Note I wouldn't use this, or any of the alternatives so far, any place I expected to get called repeatedly (e.g., in a loop). IIRC preparing a statement, either explicitly or via do is often a more expensive operation than executing it, so you'd want to pull the prepare out of your loop somehow, or else memoize the prepared statement.
HTH,
optimist
In reply to Re: Re: Re: Need an array as a function parameter
by optimist
in thread Need an array as a function parameter
by stonecolddevin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |