2013-07-06 114 views
0

我已經在Windows 8上運行的x64下面的代碼:的SQLite 3數據庫沒有更新

import sqlite3 as db 

conn = db.connect('test.db') 
cursor = conn.cursor() 
cursor.execute("create table films(title text, year text, director text)") 
print("table created") 

cursor.execute('insert into films values("Annie Hall","1977","Woody Allen")') 
cursor.execute('insert into films values("The Godfather","1972","Francis Ford Coppola")') 

conn.close() 

檢查所創建的SQLite的文件(test.db的)(使用sqlite command-line shell,以測試是否有條目通過代碼創建的表)不顯示任何內容。任何幫助?

回答

1

您必須在conn.close()之前致電conn.commit()

如果你想自動提交模式下,你已經把isolation_level=None作爲論據db.connect()電話。

+0

根據定義DB-API的[PEP](http://www.python.org/dev/peps/pep-0249/),只有當他以事務模式運行並且對數據庫事務的支持是可選的時, – hd1

+0

如果我沒有記錯'isolation_level'默認不是* autocommit *模式。無論如何,我只是按照手動示例:http://docs.python.org/3/library/sqlite3.html :) –

0

spec表示光標可以只讀,或者您的數據庫可能處於事務模式。我也不清楚你爲什麼使用execute而沒有在準備好的聲明中指定你的參數,如cursor.execute('insert into films values(?,?,?)', 'Annie Hall', '1977', 'Woody Allen')。嘗試這些想法,我沒有看到你的代碼中頭頂的任何錯誤,但我只是建議最佳實踐。