加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么$dbh-> do(‘VACUUM’)失败了Perl的DBD :: SQLite?

发布时间:2020-12-15 23:26:13 所属栏目:大数据 来源:网络整理
导读:我想在Perl上的SQLite数据库上的特定时间做VACUUM,但它总是说 DBD::SQLite::db do failed: cannot VACUUM from within a transaction 那我该怎么做? my %attr = ( RaiseError = 0,PrintError = 1,AutoCommit = 0 );my $dbh = DBI-connect('dbi:SQLite:dbnam
我想在Perl上的SQLite数据库上的特定时间做VACUUM,但它总是说

DBD::SQLite::db do failed: cannot VACUUM from within a transaction

那我该怎么做?

my %attr = ( RaiseError => 0,PrintError => 1,AutoCommit => 0 );
my $dbh = DBI->connect('dbi:SQLite:dbname='.$file'','',%attr) 
    or die $DBI::errstr;

我正在使用AutoCommit => 0.错误发生在:

$dbh->do('DELETE FROM soap');
$dbh->do('DELETE FROM result');
$dbh->commit; 
$dbh->do('VACUUM');

解决方法

我假设你有AutoCommit =>连接调用中为0,因为以下工作原理:

#!/usr/bin/perl

use strict;
use warnings;

use DBI;

my $dbh = DBI->connect('dbi:SQLite:test.db',undef,{ RaiseError => 1,AutoCommit => 1}
);

$dbh->do('VACUUM');

$dbh->disconnect;

您不必放弃能够进行VACUUM的事务:您可以使用以下内容以便为VACUUM打开AutoCommit,并在VACUUM之后将AutoCommit状态恢复为它的状态.如果未设置RaiseError,请添加错误检查.

sub do_vacuum {
    my ($dbh) = @_;
    local $dbh->{AutoCommit} = 1;
    $dbh->do('VACUUM');
    return;
}

叫它:

do_vacuum($dbh);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读