在 python sqlite3 中使用保存点

发布于 2024-08-17 00:57:36 字数 1170 浏览 8 评论 0原文

我正在尝试将保存点与 python 2.6 中内置的 sqlite3 模块一起使用。每次我尝试释放或回滚保存点时,我总是收到一个 OperationalError: no such savepoint。我缺少什么?

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

从这段代码:

import sys
import sqlite3

print 'python version:', sys.version
print 'PySQLite version:', sqlite3.version
print 'sqlite3 version:', sqlite3.sqlite_version
print

conn = sqlite3.connect('db_spDemo.db')
conn.isolation_level = "DEFERRED"

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("release savepoint spTest;")

    conn.execute("insert into example values (?, ?);", (5,205))

I'm attempting to use savepoints with the sqlite3 module built into python 2.6. Every time I try to release or rollback a savepoint, I always recieve an OperationalError: no such savepoint. What am I missing?

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

from this code:

import sys
import sqlite3

print 'python version:', sys.version
print 'PySQLite version:', sqlite3.version
print 'sqlite3 version:', sqlite3.sqlite_version
print

conn = sqlite3.connect('db_spDemo.db')
conn.isolation_level = "DEFERRED"

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("release savepoint spTest;")

    conn.execute("insert into example values (?, ?);", (5,205))

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

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

发布评论

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

评论(2

九八野马 2024-08-24 00:57:37

这是 pysqlite 中的错误,请参阅pysql 问题跟踪器Python 问题跟踪器

This is a bug in pysqlite, see the pysql issue tracker and the python issue tracker.

半﹌身腐败 2024-08-24 00:57:36

这似乎是 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

这是一个令人不满意的答案,我没有在 sqlite3 模块文档中看到任何相关内容(我也没有尝试查看源代码)。但我希望它能帮助你找到正确的方向。

This appears to be a result of how the sqlite3 module behaves with that isolation level.

This works, notice the two changes:

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))

Output:

$ 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

This is an unsatisfactory answer, and I didn't see anything relevant in the sqlite3 module docs (nor did I try to take a look at the source). But I hope it helps you find the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文