如何在循环中更新SELECT返回的行?

Dio*_*lis 5 sql sqlite perl dbd

我在SQL SELECT语句返回的行上有一个循环,并且在对行的数据进行一些处理之后,我有时想要更新行的值.循环体中的处理非常重要,我无法在SQL中编写它.当我尝试为所选行执行UPDATE时,我得到一个错误(在Perl的DBD :: SQLite :: st执行失败:数据库表被锁定).是否有可读,高效且便携的方式来实现我想要做的事情?如果不这样做,是否有DBD或SQLite特定的方法呢?

显然,我可以在单独的数据结构中推送更新并在循环之后执行它们,但是我讨厌代码看起来那样.

如果您有兴趣,这里是相应的Perl代码.

my $q = $dbh->prepare(q{
  SELECT id, confLoc FROM Confs WHERE confLocId ISNULL});
$q->execute or die;
my $u = $dbh->prepare(q{
  UPDATE Confs SET confLocId = ? WHERE id = ?});
while (my $r = $q->fetchrow_hashref) {
    next unless ($r->{confLoc} =~ m/something-hairy/);
    next unless ($locId = unique_name_state($1, $2));
    $u->execute($locId, $r->{id}) or die;
}
Run Code Online (Sandbox Code Playgroud)

Sin*_*nür 6

暂时启用AutoCommit:

sqlite> .header on
sqlite> select * from test;
field
one
two
#!/usr/bin/perl

use strict;
use warnings;

use DBI;

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

test_select_with_update($dbh);

sub test_select_with_update {
    my ($dbh) = @_;
    local $dbh->{AutoCommit} = 1;
    my $q = $dbh->prepare(q{SELECT field FROM test});
    my $u = $dbh->prepare(q{UPDATE test SET field = ? WHERE field = ?});
    $q->execute or die;
    while ( my $r = $q->fetchrow_hashref ) {
        if ( (my $f = $r->{field}) eq 'one') {
            $u->execute('1', $f) or die;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

代码运行后:

sqlite> .header on
sqlite> select * from test;
field
1
two