2010-01-10 64 views
5

我試圖使用內置到python 2.6中的sqlite3模塊使用保存點。每次我嘗試釋放或回滾保存點時,我總是會收到一個OperationalError: no such savepoint。我錯過了什麼?在python 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 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)) 
+1

相關:http://stackoverflow.com/questions/1654857/ – 2010-01-10 08:06:19

回答

3

這似乎是的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的模塊文檔相關的任何事情(也沒有我試着看看源代碼)。但我希望它能幫助你找到正確的方向。

+0

我從來沒有考慮嘗試它沒有任何外部事務級別,因爲SQL文檔表明保存點可以與事務一起使用。這暗示了我可能需要一個特定的編譯指令才能使它們一起工作!謝謝你給我一個更好的調查方向.. – 2010-01-11 16:22:51