如何使用Perl的DBI模块将并行数组中的值插入数据库中?

shu*_*ter 0 perl dbi

我需要使用Perl的DBI模块在数据库中插入值。我已解析的文件,以获得这些值,因此这些值出现在一个阵列中,比方说@array1@array2@array3。我知道如何一次插入一个值,但不能从数组中插入。

我知道一次插入一个值:

$dbh = DBI->connect("dbi:Sybase:server=$Srv;database=$Db", "$user", "$passwd") or die "could not connect to database";
$query= "INSERT INTO table1 (id, name, address) VALUES (DEFAULT, tom, Park_Road)"; 
$sth = $dbh->prepare($query) or die "could not prepare statement\n";
$sth-> execute or die "could not execute statement\n $command\n";
Run Code Online (Sandbox Code Playgroud)

我不确定是否有包含ID的array1,包含名称的array2和包含地址的array3,如何插入值。

bis*_*ish 5

由于您具有并行数组,因此可以采用execute_array的优势

my $sth = $dbh->prepare('INSERT INTO table1 (id, name, address) VALUES (?, ?, ?)');
my $num_tuples_executed = $sth->execute_array(
    { ArrayTupleStatus => \my @tuple_status },
    \@ids,
    \@names,
    \@addresses,
);
Run Code Online (Sandbox Code Playgroud)

请注意,这是文档中的截断(略有修改)的示例。如果您决定使用此功能,则肯定要检查其余内容。