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

时间:2009-08-20 01:33:18

标签: sql perl sqlite dbi vacuum

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

  

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

2 个答案:

答案 0 :(得分:11)

我假设你在连接通话中有AutoCommit => 0,因为以下工作:

#!/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和{{1}之后启用VACUUM } VACUUM状态将恢复为原来的状态。如果您未设置AutoCommit,请添加错误检查。

RaiseError

称之为:

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

答案 1 :(得分:1)

默认情况下,DBI已启用自动提交。在连接期间将其关闭:

my $dbh = DBI->connect($dsn, $user, $pass, { AutoCommit => 0 });