我尝试使用python脚本创建、编辑和读取sqlite文件。我的代码是一个服务器-客户机模型,客户机在收到来自服务器的命令时向数据库写入数据。在
来自服务器的每个命令都在一个单独的线程上接收,以提供并行操作。在
除非系统重新启动,否则客户端不会重新启动,但服务器程序会在用户需要时启动。在
现在我的问题出现了,因为sqlite for python不是线程安全的。所以我有一个到数据库的使用者队列,用于所有的写操作。在
我不能提供代码,因为它非常长,而且很难解耦并提供完整的工作副本。在
但错误是:
def writedata(self, _arg1, _arg2, _arg3):
# self.sql_report is the fully qualified path of sqlite file
db = sqlite3.connect(self.sql_report)
c = db.cursor()
res = c.execute("Select id from State")
listRowId = []
for element in res.fetchall():
listRowId.append(element[0])
self.currentState = max(listRowId)
sql = "INSERT INTO Analysis (type, reason, Component_id, State_id) VALUES (?, ?, ifnull((SELECT id from `Component` WHERE name = ? LIMIT 1), 1), ?)"
# call to the write queue.
Report.strReference.writeToDb(sql, [(_arg1, _arg3, _arg2, self.currentState)])
我收到的错误是
^{pr2}$第81行:这是:
#first line of the snippet code pasted above
db = sqlite3.connect(self.sql_report)
我不知道为什么会出现这个错误。但需要注意的一点是,只有在服务器运行几次之后才会出现此错误。在
错误正是它所说的。您正在传递
self.sql_report
作为要使用的字符串数据库文件名,但在调用时它不是字符串。在您需要找出它的真正含义,这是标准的Python调试。用你平时用的任何东西。这里还有一个建议,它将打印它的内容并放入一个交互式调试器中,以便您进一步检查。在
try: db = sqlite3.connect(self.sql_report) except ValueError: print (repr(self.sql_report)) import pdb; pdb.set_trace() raise
相关问题 更多 >
编程相关推荐