sqlite,从更新中获取字段

时间:2011-04-07 14:25:56

标签: bash sqlite

我有一个带有表测试的sqlite数据库。有几个进程正在从bash访问这个数据库。该表包含以下字段:

CREATE TABLE mytable (id NUMERIC,
start JULIAN,
finish JULIAN)

我通过以下方式获得了一个唯一的ID:

id=$(sqlite test.db <<EOF
BEGIN EXCLUSIVE;
SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1;
COMMIT;
EOF
)

我的问题是,如何更新字段以:

开头
UPDATE mytable set start=julianday('now') where id="SELECTED ID FROM ABOVE";

在同一声明中?

1 个答案:

答案 0 :(得分:1)

根据您在上面提供的评论,我的解决方案看起来像下面(在perl中使用原始DBI连接,我也没有做很多错误检查或其他任何事情,你可能应该这样做) :

my $dbh = DBI->connect(...);
$dbh->do("BEGIN EXCLUSIVE");
my $stm = $dbh->prepare("SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1");
$stm->execute();
my $row = $stm->fetchrow_hashref();
my $id = undef;
if ( $row ) {
    $id = $row->{ID};
    my $ustm = $dbh->prepare("UPDATE mytable set start=julianday('now') where id=?");
    $ustm->execute($id);
}
$dbh->do("COMMIT");
# Still have the id at this point.