Sven Deckers
Sven Deckers

Reputation: 155

insert 1 row with a lot of columns using an array in Perl DBI

I would like to insert a single row through perl dbi, but this row has A LOT of columns. My query would be smt like this :

$sth->prepare("insert into bigtable (col1, col2, ..., col25) values (?, ?, ..., ?)");
$sth->execute($val1, $val2, ..., $val25);

But I want it cleaner. If I have an array, @myArray, containing these 25 variables, is there a way to do smt like this :

$sth->prepare("insert into bigtable (col1, col2, ..., col25) values (?, ?, ..., ?)");
$sth->execute(@myArray);

?

Upvotes: 2

Views: 935

Answers (1)

TLP
TLP

Reputation: 67900

You can do:

my $placeholders = join ", ", ("?") x @array;
$sth->prepare("insert into bigtable (col...) values ($placeholders)");
$sth->execute(@array);

Upvotes: 4

Related Questions