简体   繁体   English

如何将数组的一部分插入MySQL?

[英]How can I insert parts of an array into MySQL?

I have an array in Perl like the following (only much bigger). 我在Perl中有一个像下面这样的数组(只有更大)。 My array is called @sqlInsert 我的数组称为@sqlInsert

Element1 ABC Element1 ABC

Element2 ABC Element2 ABC

Element3 ABC Element3 ABC

I want to fill a MySQL table with the information in this data. 我想用此数据中的信息填充MySQL表。 So really, I want to have one row going: 所以说真的,我想排一行:

 for ($count=0; $count<@arrayInsert; $count++) {
      INSERT INTO table values ("sqlInsert[$i]","sqlInsert[$i+1]","sqlInsert[$i+2]","sqlInsert[$i+3]")
 }

Any pointers? 有指针吗?

Use DBI placeholders and... 使用DBI占位符和...

...an array slice: ...数组切片:

 my $sth = $dbh->prepare( 'INSERT INTO table VALUES ( ?, ?, ?, ? )' );

 $sth->execute( @sqlInsert[ $n .. $m ] );

...or a splice: ...或接头:

 while( @sqlInsert ) {
      $sth->execute( splice @sqlInsert, 0, $length, () );
      }

It might be easier to create a data structure, such as an array of arrays, that already groups the elements for each structure: 创建已经对每个结构的元素进行分组的数据结构(例如数组数组)可能会更容易:

 foreach my $ref ( @sqlInsert ) {
      $sth->execute( @$ref );
      } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM