in reply to Proper Syntax for DBI multiple row insert
While this is standard SQL, or at least widely-enough supported that PostgreSQL also allows a single INSERT to add multiple rows, this builds an SQL query dynamically, which is generally a bad habit, since it can create a risk for SQL injection attacks if user-provided data is used to build the query.
Unless you have unusual requirements, a better way to achieve this is to prepare a statement that inserts a single row and then execute that statement once for each row, like the DBI manual(section "Outline Usage") suggests: (untested)
my @records = ( ['value1', 'value2'], ...) ; my $sth = $dbh->prepare("INSERT INTO $table (field1, field2) VALUES (? +,?)"); foreach my $valuesref (@records) { $sth->execute(@$valuesref) }
The other reason to do it this way is that the statement handle can be kept around and reused if records are to be inserted into the same table more than once. You can use $dbh->prepare_cached instead of $dbh->prepare, but see the caveats that are explained in the DBI manual.
|
|---|