SQLite 3数据库未更新

2024-03-29 12:09:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Windows 8 x64上运行以下代码:

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文件(测试.db)(使用sqlite command-line shell测试通过代码创建的表中是否有条目)没有显示任何内容。有什么帮助吗?你知道吗


Tags: 代码textimportexecutedbwindowstableconn
2条回答

您必须在conn.close()之前调用conn.commit()。你知道吗

如果你想要自动提交模式,你必须把isolation_level=None作为db.connect()调用的参数。你知道吗

spec表示游标可以是只读的,或者您的数据库可能处于事务模式。我也不清楚为什么在使用execute时没有在准备好的语句中指定参数,如cursor.execute('insert into films values(?,?,?)', 'Annie Hall', '1977', 'Woody Allen')。试试这些想法,我看不出你的代码中有任何错误,但我只是在建议最佳实践。你知道吗

相关问题 更多 >