Ever thought about (ab)using interpolation and the list-seperator variable to simplify things visually? Or perhaps some rudimentary checking of expected arguments?
sub insert { # Untested, but you get the idea ...
my $dbh = shift or warn("No database handle"), return;
my $table = shift or warn("No table given"), return;
my @columns = @_ or warn("No columns given"), return;
my $sql;
{
local $" = ","; # interpolation voodoo :-)
my @values = ("?") x @columns;
$sql = "INSERT INTO $table (@columns) VALUES (@values)"; # s/my //
}
$dbh->prepare($sql) or die("Couldn't prepare $sql " . $dbh->errstr);
}
--k.
Update: per extremely below, because they've got a point. :-)
In reply to Re: SQL INSERT creator
by Kanji
in thread SQL INSERT creator
by boo_radley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |