【问题标题】:Bulk inserting to mysql DB table using perl使用 perl 批量插入 mysql 数据库表
【发布时间】:2014-11-03 20:44:11
【问题描述】:

我正在使用一个简单的 perl 脚本来填充 mysql DB 表中的数百万行。我在脚本中使用 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 行后提交。如果可能的话,有人可以告诉我怎么做。提前致谢。干杯...

【问题讨论】:

标签: mysql perl bulkinsert dbi dbd


【解决方案1】:

你必须设置然后“自动提交为零:

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

并调用所有 n 行 $dbh-&gt;commit()

更多详情请见DBI Documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2018-05-22
    • 2011-01-14
    相关资源
    最近更新 更多