4

我想VACUUM在某个时间在 Perl 下的 SQLite 数据库上做,但它总是说

DBD::SQLite::db 失败:不能从事务中 VACUUM

那么我该怎么做呢?

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');
4

2 回答 2

11

我假设您AutoCommit => 0在 connect 调用中,因为以下工作:

#!/usr/bin/perl

use strict;
use warnings;

use DBI;

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

$dbh->do('VACUUM');

$dbh->disconnect;

您不必放弃交易即可VACUUM:您可以使用以下内容,以便在状态恢复到原来状态时和之后AutoCommit打开。如果不设置,请添加错误检查以品尝。VACUUMVACUUMAutoCommitRaiseError

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

叫它:

do_vacuum($dbh);
于 2009-08-20T01:42:50.983 回答
1

DBI 默认开启自动提交。在连接期间将其关闭:

my $dbh = DBI->connect($dsn, $user, $pass, { AutoCommit => 0 });
于 2009-08-20T01:38:29.053 回答