将SQLite与APSW配合使用时出现“BusyError:cannot rollback savepoint - SQL statements in progress”(忙错误:无法回滚保存点-正在执行SQL语句)

jtw3ybtb  于 2022-12-27  发布在  SQLite
关注(0)|答案(2)|浏览(156)

我使用Python apsw绑定来处理SQLite数据库,代码如下:

with apsw.Connection(path) as t:
    c = t.cursor()
    c.execute(...)
    ... more code ...
    if c.execute(...).next()[0]:
        raise Exception

我希望with语句放置一个保存点,raise语句回滚到那个保存点(或者,如果没有什么要引发的,提交事务)。它提交得很好,但是当raise有一些东西时,它拒绝回滚,原因是:

BusyError: BusyError: cannot rollback savepoint - SQL statements in progress

我不知道该从哪里开始,据我所知,这个错误意味着有另一个连接阻止了访问,但代码看起来不是这样,如果是这样,提交时不也会失败吗?
SQLite3.7.7.1匹配apsw,Python 2.7。

deyfvvtc

deyfvvtc1#

嗯,我找到了:

if c.execute(...).next()[0]:
    raise Exception

问题是,当我用next()得到下一行时,底层游标保持活动状态,准备返回更多行,必须显式关闭它:

if c.execute(...).next()[0]:
    c.close()
    raise Exception

或隐含地通过阅读所有数据:

if list(c.execute(...))[0][0]:
    raise Exception

为了方便起见,我写了一个Python类,它 Package 了apsw.Cursor并提供了一个上下文管理器,所以我可以写:

with Cursor(connection) as c:
    c.execute(...)
tyky79it

tyky79it2#

这是SQLite本身的问题,在2012年3月的3.7.11版本中得到了修复。
挂起语句不再阻塞ROLLBACK。相反,挂起语句将在ROLLBACK之后的下一次访问时返回SQLITE_ABORT。

相关问题