简体   繁体   English

为什么我在这个简单的python sqlite3更新查询代码上遇到“数据库被锁定”错误?

[英]Why do I get a “database is locked” error on this simple python sqlite3 update query code?

Every SQLITE query I make usually goes fine, except I cannot seem to be able to update the DB. 我做的每个SQLITE查询通常都很好,除了我似乎无法更新数据库。

db = sqlite3.connect("data.db")
cursor = db.cursor()
str_datetime = str(datetime.datetime.now())
cursor.execute("""UPDATE ads SET publish_end_datetime = ? WHERE ad_code = ?""", (str_datetime, 1411671200))
db.commit() # is this commit statement even necessary?
db.close()

I get the following operational error: 我收到以下操作错误:

OperationalError Traceback (most recent call last) in () 3 str_datetime = str(datetime.datetime.now()) 4 ----> 5 cursor.execute("""UPDATE ads SET publish_end_datetime = ? WHERE ad_code = ?""", (str_datetime, 1411671200)) 6 db.commit() 7 db.close() ()中的OperationalError Traceback(最近一次调用)3 str_datetime = str(datetime.datetime.now())4 ----> 5 cursor.execute(“”“UPDATE ads SET publish_end_datetime =?WHERE ad_code =?”“ “,(str_datetime,1411671200))6 db.commit()7 db.close()

OperationalError: database is locked OperationalError:数据库已锁定

I think I've found the problem. 我想我已经找到了问题。 Whenever the DB is in use, a database.db-journal file is created in the same location. 每当使用DB时,都会在同一位置创建database.db-journal文件。 That's the main indicator that the DB is in use. 这是数据库正在使用的主要指标。 I have not found what was "using" my DB, but I've found how to solve my problem. 我还没有找到“使用”我的数据库,但我找到了如何解决我的问题。 It appears that OneDrive might have been the problem. 似乎OneDrive可能是问题所在。 I copied my data.db on the desktop, outisde of OneDrive, and I finally have been able to get the update statement to work. 我将我的data.db复制到桌面上,OneDrive的外部,我终于能够使更新语句工作了。

I hope it helps other people. 我希望它能帮助其他人。

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

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