在python sqlite3中使用保存点
我正试图将保存点与Python2.6中内置的sqlite3模块一起使用。每次尝试释放或回滚保存点时,我总是收到一个在python sqlite3中使用保存点,python,sqlite,savepoints,Python,Sqlite,Savepoints,我正试图将保存点与Python2.6中内置的sqlite3模块一起使用。每次尝试释放或回滚保存点时,我总是收到一个操作错误:没有这样的保存点。我错过了什么 python version: 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] PySQLite version: 2.4.1 sqlite3 version: 3.6.11 Traceback (most recent call
操作错误:没有这样的保存点。我错过了什么
python version: 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32)
[GCC 4.0.1 (Apple Inc. build 5493)]
PySQLite version: 2.4.1
sqlite3 version: 3.6.11
Traceback (most recent call last):
File "spDemo.py", line 21, in <module>
conn.execute("release savepoint spTest;")
sqlite3.OperationalError: no such savepoint: spTest
这似乎是sqlite3模块在该隔离级别下的行为的结果
这是可行的,请注意两个变化:
import sys
import sqlite3
print 'python version:', sys.version
print 'PySQLite version:', sqlite3.version
print 'sqlite3 version:', sqlite3.sqlite_version
print
conn = sqlite3.connect('shane.sqlite')
conn.isolation_level = None # CHANGED
with conn:
conn.execute("create table example (A, B);")
with conn:
conn.execute("insert into example values (?, ?);", (0,200))
conn.execute("savepoint spTest;")
conn.execute("insert into example values (?, ?);", (1,201))
conn.execute("insert into example values (?, ?);", (2,202))
conn.execute("rollback to savepoint spTest;") # CHANGED
conn.execute("insert into example values (?, ?);", (5,205))
输出:
$ python shane-sqlite3.py && sqlite3 shane.sqlite 'select * from example;'
python version: 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3]
PySQLite version: 2.4.1
sqlite3 version: 3.6.10
0|200
5|205
$python shane-sqlite3.py&&sqlite3 shane.sqlite'select*from example;'
python版本:2.6.2(Release26Maint,2009年4月19日,01:56:41)
[GCC 4.3.3]
PySQLite版本:2.4.1
sqlite3版本:3.6.10
0|200
5|205
这是一个不令人满意的答案,我在sqlite3模块文档中没有看到任何相关内容(我也没有尝试查看源代码)。但我希望它能帮助您找到正确的方向。这是pysqlite中的一个bug,请参阅和。相关:我知道这是一个老问题,但根据文档,您不能将开始/提交事务与保存点的查找/搜索单词“nest”或以开头的段落混为一谈“使用BEGIN…COMMIT创建的事务不嵌套。“我从来没有考虑过在没有任何外部事务级别的情况下尝试它,因为SQL文档表明保存点可以与事务一起使用。这向我暗示,可能我需要某个pragma来使所有事务一起工作!感谢您为我提供了更多调查的好方向。”。。
$ python shane-sqlite3.py && sqlite3 shane.sqlite 'select * from example;'
python version: 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3]
PySQLite version: 2.4.1
sqlite3 version: 3.6.10
0|200
5|205