Perl插入MySQL数据库

Perl插入MySQL数据库,perl,dbi,Perl,Dbi,我所拥有的: MySQL数据库 有9列的表 使用需要插入到表中的值生成的数组 注: 自动生成的数组每次都有不同的长度,但长度不会超过表中的列数 列名将类似于field1、field2、field3等,其中名称将始终与单词field在一起,然后后跟一个数字 我认为这可以使用Perl的map函数来完成,但我不太擅长使用这个函数,需要一些指导 我想这样做: while ( (@Row) = $sql_stmt_h->fetchrow_array() ) { my $sql=" I

我所拥有的:

  • MySQL数据库
  • 有9列的表
  • 使用需要插入到表中的值生成的数组
注: 自动生成的数组每次都有不同的长度,但长度不会超过表中的列数

列名将类似于field1、field2、field3等,其中名称将始终与单词field在一起,然后后跟一个数字

我认为这可以使用Perl的
map
函数来完成,但我不太擅长使用这个函数,需要一些指导

我想这样做:

while ( (@Row) = $sql_stmt_h->fetchrow_array() ) {
 my $sql="
     INSERT INTO tablename (field$x) 
     VALUES (map function here ... which also needs to increment the $x in field$x so that it moves onto the next column name which would be field2 if we put the first value in field1. )";
}

创建Insert语句以使用数据的占位符插入所有字段

my $sql="
 INSERT INTO tablename (field1 field2 field3 field4 field5 field6 field7 field8 field9 ) 
 VALUES (? ? ? ? ? ? ? ? ?)";
my $insert_sth = $dbh->prepare($sql);
然后做一些类似的事情

my @new_data = get_array_with_random_length();
splice(@new_data, @new_data, 0, undef x (9 - @new_data)); #pad @newdata to 9 elements w/ undefs (DBI will xlate to NULL)
$insert_sth->execute(@new_data);