简体   繁体   English

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

[英]Python, Sqlite not saving results on the file

I have this code in Python:我在 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()

But seems is never saving to the database, there is always the same elements on the db as if it was not saving anything.但似乎永远不会保存到数据库中,数据库中总是有相同的元素,就好像它没有保存任何东西一样。

On the other side If I try to access the db file via sqlite it I got this error:另一方面,如果我尝试通过 sqlite 访问 db 文件,则会出现此错误:

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

I found on some other answers to use conn.commit instead of conn.commit() but is not changing the results.我在其他一些答案中发现使用conn.commit而不是conn.commit()但没有改变结果。

Any idea?任何的想法?

BINGO.答对了。 people.人们。 I Had the same problem, One of thr reasons where very simple.我遇到了同样的问题,其中一个原因很简单。 I`am using debian linux, error was我正在使用 debian linux,错误是

Unable to open database "people.db": file is encrypted or is not a database无法打开数据库“people.db”:文件已加密或不是数据库

database file was in the same dir than my python script connect line was数据库文件与我的 python 脚本连接线位于同一目录中
conn = sqlite3.connect('./testcases.db')

I changed this我改变了这个

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

. . No dot and slash.没有点和斜线。 Error Fixed.错误已修复。 All works所有作品

If someone think it is usefull, you`re welcome如果有人觉得有用,不客气

This seems to work alright for me ("In database" increases on each run):这似乎对我有用(每次运行时“在数据库中”都会增加):

import random, sqlite3

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

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

for x in xrange(5):
    cursor.execute('insert into people VALUES (?, "test")', (random.randint(1, 10000),))
conn.commit()

cursor.execute("select count(*) from people")
print "In database:", cursor.fetchone()[0]

You should commit after making changes ie:您应该在进行更改后提交,即:

myDatabase.commit()

can you open the db with a tool like sqlite administrator?您可以使用 sqlite 管理员之类的工具打开数据库吗? this would proove thedb-format is ok.这将证明 db 格式是可以的。 if i search for that error the solutions point to version issues between sqlite and the db-driver used.如果我搜索该错误,则解决方案指向 sqlite 和所使用的 db-driver 之间的版本问题。 maybe you can chrck your versions or AKX could post the working combination.也许你可以 chrck 你的版本或者 AKX 可以发布工作组合。

regards,khz问候,khz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM