2013-07-06 96 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')。尝试这些想法,我没有看到你的代码中头顶的任何错误,但我只是建议最佳实践。