我有一个使用几个sql数据库存储数据的程序。我有一个管理各种sql函数的类,例如获取值,整个表或仅更新值。在我运行使用UPDATE的函数之前,所有过程都可以正常工作。我执行UPDATE命令并尝试提交更改,并且数据库始终处于锁定状态。我在自定义sql类中具有的每个函数都有
cursor.close
database.close
因此数据库连接仍处于打开状态应该不会有任何问题。我是否缺少该语法中未正确连接到数据库的内容?我使用了额外的打印语句,试图找出问题出在哪里,因此可以忽略它们。
import sqlite3 as db
import os
databaseName = "site"
class MassDb:
def __init__(self,databaseName):
super(MassDb, self).__init__()
print("Current Directory: ",os.getcwd())
self.databaseName = databaseName
def updateValue(self, location, metric, input_value):
print("OPEN CONNECTION UPDATE - running updateValue: ",location, metric, input_value)
if self.databaseName == "site":
try:
siteConn = db.connect("site_data.db")
siteCursor = siteConn.cursor()
siteCursor.execute("UPDATE sites SET " + metric + " = ? WHERE LOCATI ON = ?", (input_value, location))
siteConn.commit()
except:
print("UPDATE FAILED")
finally:
siteCursor.close
siteConn.close
elif self.databaseName == "comp":
try:
compConn = db.connect("comp_data.db")
compCursor = compConn.cursor()
compCursor.execute("UPDATE competitors SET " + metric + " = ? WHERE NAME = ?", (input_value, location))
compConn.commit()
except:
print("UPDATE FAILED")
finally:
compCursor.close
compConn.close
print("CLOSED CONNECTION UPDATE - Update Connection Closed")
else:
print("Update Error")
MassDb("site").updateValue("Location", "CURRENT_SCORE", "100")
答案 0 :(得分:0)
正如@roganjosh所评论的那样,我的问题是我没有正确关闭数据库。如果
commit()
使用,则无需关闭数据库。但是,
cursor.close()
和
conn.close()
需要这样写。省略括号就像是在引用属性,而不是方法。为了执行close方法,必须存在()。现在似乎很明显,但当时我还不知道。希望这对其他人也有帮助。