Perl DBI MySQL:如何运行多个查询/语句
作者:互联网
参见英文答案 > Perl DBI – run SQL Script with multiple statements 4个
目前,我在MYSQL上运行多个语句,如下所示;
my $sth1 = $dbh->prepare("ALTER TABLE whatever....");
my $sth2 = $dbh->prepare("UPDATE whatever....");
my $sth3 = $dbh->prepare("ALTER TABLE whatever....");
my $sth4 = $dbh->prepare("DROP TABLE whatever....");
my $sth5 = $dbh->prepare("DROP TABLE whatever....");
$sth1->execute;
$sth1->finish;
$sth2->execute;
$sth2->finish;
$sth3->execute;
$sth3->finish;
$sth4->execute;
$sth4->finish;
$sth5->execute;
$sth5->finish;
这段代码工作正常.
但是,我有大约50多个这样的查询.所以你可以想象上面这些线的大小.我上面粘贴的只是5个查询.
题:
使用Perl DBI运行多个MySQL查询/语句是否有更好的优雅方式?
解决方法:
至少,你应该迭代你的sql字符串.也是一个好主意添加或死你的执行方法:
my @sql = (
q{ALTER TABLE whatever....},
q{UPDATE whatever....},
q{ALTER TABLE whatever....},
q{DROP TABLE whatever....},
q{DROP TABLE whatever....},
);
for (@sql) {
my $sth = $dbh->prepare($_);
$sth->execute or die $dbh->errstr;
}
标签:mysql,perl,dbi 来源: https://codeday.me/bug/20190713/1449738.html