ai didi

Python,Sqlite 不在文件中保存结果

转载 作者:太空狗 更新时间:2023-10-30 02:48:44 24 4
gpt4 key购买 nike

我在 Python 中有这段代码:

conn = sqlite3.connect("people.db")
cursor = conn.cursor()

sql = 'create table if not exists people (id integer, name VARCHAR(255))'
cursor.execute(sql)
conn.commit()

sql = 'insert into people VALUES (3, "test")'
cursor.execute(sql)
conn.commit()

sql = 'insert into people VALUES (5, "test")'
cursor.execute(sql)
conn.commit()

print 'Printing all inserted'
cursor.execute("select * from people")
for row in cursor.fetchall():
print row

cursor.close()
conn.close()

但似乎永远不会保存到数据库中,数据库中总是有相同的元素,就好像它没有保存任何东西一样。

另一方面,如果我尝试通过 sqlite 访问 db 文件,则会出现此错误:

Unable to open database "people.db": file is encrypted or is not a database

我在其他一些答案中发现使用 conn.commit 而不是 conn.commit() 但没有改变结果。

有什么想法吗?

最佳答案

宾果!人们!我有同样的问题。原因之一非常简单。我正在使用 debian linux,错误是

Unable to open database "people.db": file is encrypted or is not a database

数据库文件与我的 python 脚本在同一个目录中连接线是
conn = sqlite3.connect('./testcases.db')

我改变了这个

conn = sqlite3.connect('testcases.db')

!没有点和斜线。错误已修复。所有作品

如果有人觉得有用,不客气

关于Python,Sqlite 不在文件中保存结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11807700/

24 4 0
文章推荐: python - 使模型引用一对多抽象模型
文章推荐: python - wxpython 在静态位图上绘图
文章推荐: python - 不要用完列表索引的最佳方法
文章推荐: python - Pyglet 将一组图像绘制成更大的图像
太空狗
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com