gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:30:52 29 4
gpt4 key购买 nike

我想在某个时间在 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, undef,
{ RaiseError => 1, AutoCommit => 1}
);

$dbh->do('VACUUM');

$dbh->disconnect;

您不必放弃交易就可以 VACUUM : 您可以使用以下内容 AutoCommitVACUUM 打开在 VACUUM 之后AutoCommit状态恢复到原来的状态。如果您不设置 RaiseError,请添加错误检查。 .

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

调用它:

do_vacuum($dbh);

关于sql - 为什么 $dbh->do ('VACUUM' ) 在 Perl 的 DBD::SQLite 中失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1303514/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com