簡體   English   中英

使用perl批量插入mysql數據庫表

[英]Bulk inserting to mysql DB table using perl

我使用一個簡單的perl腳本來在mysql數據庫表中填充數百萬行。 我在腳本中使用perl DBI和DBD :: mysql。 示例代碼如下

my $dbh = DBI->connect(<DB INFO>);
my $sth;
my $insert_com = "INSERT INTO TAB1 VALUES(?,?,?,?,?)";
for (1..50000000){

   $sth = $dbh->prepare($insert_com);
   $sth->execute(<val1>,<val2>,<val3>,<val4>,<val5>);

}

根據上面的代碼,我認為為循環的每次迭代發送一個提交。 我的問題是,是否可以每n次迭代發送一次提交? 即在向表中插入n行后提交。 如果可能,有人可以告訴我如何。 提前致謝。 干杯......

你必須設置“AutoCommit為零:

$dbh = DBI->connect($dsn, $user, $password,
                      { RaiseError => 1, AutoCommit => 0 });

並調用所有n行$dbh->commit()

有關詳細信息,請參閱DBI文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM