简体   繁体   中英

How can I insert parts of an array into MySQL?

I have an array in Perl like the following (only much bigger). My array is called @sqlInsert

Element1 ABC

Element2 ABC

Element3 ABC

I want to fill a MySQL table with the information in this data. 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...

...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 );
      } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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