我有一个 Perl 数组,如下所示(只是更大)。我的数组称为@sqlInsert
Element1 A B C
Element2 A B C
Element3 A B C
我想用这些数据中的信息填充 MySQL 表。所以真的,我想要一行:
for ($count=0; $count<@arrayInsert; $count++) {
INSERT INTO table values ("sqlInsert[$i]","sqlInsert[$i+1]","sqlInsert[$i+2]","sqlInsert[$i+3]")
}
有什么指点吗?
最佳答案
使用DBI placeholders还有...
...数组切片:
my $sth = $dbh->prepare( 'INSERT INTO table VALUES ( ?, ?, ?, ? )' );
$sth->execute( @sqlInsert[ $n .. $m ] );
...或拼接:
while( @sqlInsert ) {
$sth->execute( splice @sqlInsert, 0, $length, () );
}
创建一个已经对每个结构的元素进行分组的数据结构(例如数组的数组)可能会更容易:
foreach my $ref ( @sqlInsert ) {
$sth->execute( @$ref );
}
关于mysql - 如何将数组的一部分插入MySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4101853/