2014-09-10 197 views
0

我正在使用一个简单的perl脚本来混淆mysql数据库表中的数百万行。我在脚本中使用了perl DBI和DBD :: mysql。下面的示例代码使用perl批量插入到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行之后提交。如果可能的话,有人可以告诉我如何。提前致谢。干杯......

+0

可能重复单行插入20K行](http://stackoverflow.com/questions/22164070/mysql-insert-20k-rows-in-single-insert) – 2014-09-10 09:09:25

+0

以'$ sth = $ dbh-> prepare($ insert_com);'语句出循环 – 2016-05-11 14:01:10

回答

2

你必须设置,则“自动提交到零。

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

,并呼吁所有n行$dbh->commit()

详情请参阅DBI Documentation [MySQL的插入的

+0

谢谢一堆... – afwsl2003 2014-09-10 11:59:42