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

发布于 2024-08-02 20:00:07 字数 524 浏览 9 评论 0原文

我想在 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');

I want to do VACUUM at a certain time on a SQLite database under Perl, but it always says

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

So how do I do this?

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

I am using AutoCommit => 0. And the error happens while:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2024-08-09 20:00:08

我假设您有 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 code> 为 VACUUM 打开,并且在 VACUUM 之后,AutoCommit 状态将恢复到原来的状态。如果您未设置 RaiseError,请添加错误检查。

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

称之为:

do_vacuum($dbh);

I am assuming you have AutoCommit => 0 in the connect call because the following works:

#!/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;

You don't have to give up on transactions to be able to VACUUM: You can use the following so that AutoCommit is turned on for VACUUM and after the VACUUM the AutoCommit state is reverted back to whatever it was. Add error checking to taste if you do not set RaiseError.

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

Call it:

do_vacuum($dbh);
猫九 2024-08-09 20:00:08

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

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

The DBI has autocommit turned on by default. Turn it off during the connect:

my $dbh = DBI->connect($dsn, $user, $pass, { AutoCommit => 0 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文