Python sqlite错误:值错误:数据库参数必须是字符串或APSW连接对象

68bkxrlz  于 2022-12-27  发布在  SQLite
关注(0)|答案(1)|浏览(118)

我正在尝试使用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)])

我收到的错误是

File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "/usr/lib/python2.6/threading.py", line 477, in run
self.__target(*self.__args, **self.__kwargs)
File "/nameoffile", line 357, in nameofmethod
Report().writedata("test","text","analysis")
File "./nameofscript/Report.py", line 81, in writedata
ValueError: database parameter must be string or APSW Connection object

第81行:如下所示:

#first line of the snippet code pasted above
 db = sqlite3.connect(self.sql_report)

我不知道为什么会出现这个错误,但是有一点需要注意,这个错误只会在服务器运行几次之后出现。

iyfamqjs

iyfamqjs1#

错误正是它所说的。您正在传递self.sql_report作为要使用的字符串数据库文件名,但在进行调用时它不是字符串。
你需要找出它到底是什么,这是标准的Python调试。使用你通常使用的任何东西。这里还有一个建议,它将打印它是什么,并放入一个交互式调试器,以便你可以进一步检查。
try: db = sqlite3.connect(self.sql_report) except ValueError: print (repr(self.sql_report)) import pdb; pdb.set_trace() raise

相关问题