我有一个功能,您可以在下面看到。如果运行此函数,我将收到您在标题中看到的错误,您能帮助我吗?
不久前我能够用锁解决这个问题,但现在它们不起作用。我知道这与我的连接有关,但我不知道如何解决这个问题
def insertNewValues(self,uselessInput):
self.lock.acquire()
self.connection.reconnect()
mycursor = self.connection.cursor()
query=f"SELECT {self.roomSelect(self.roomNames)} ,time FROM alldata ORDER BY ID DESC LIMIT 1"
mycursor.execute(query,)
records = mycursor.fetchall()
oldValues=[elem for elem in records[0]]
time=self.addMin(oldValues[-1])
del oldValues[-1]
newValues=[self.randomIncreaseDecrease(elem) for elem in oldValues]
query=f"DELETE FROM temp_minutes LIMIT 1"
mycursor.execute(query,)
self.connection.commit()
mycursor.close()
mycursor = self.connection.cursor()
query=f"""
BEGIN;
INSERT INTO alldata ({self.roomSelect(self.roomNames)}, time)
VALUES {*newValues,str(time)};
INSERT INTO temp_minutes ({self.roomSelect(self.roomNames)}, time)
VALUES {*newValues,str(time)};
COMMIT;
"""
mycursor.execute(query,)
self.connection.commit()
mycursor.close()
self.lock.release()
解决方案是不能同时执行 2 个查询。所以你基本上必须将两者分开,如下所示:
def insertNewValues(self,uselessInput):
self.lock.acquire()
self.connection.reconnect()
mycursor = self.connection.cursor()
query=f"SELECT {self.roomSelect(self.roomNames)} ,time FROM alldata ORDER BY ID DESC LIMIT 1"
mycursor.execute(query,)
records = mycursor.fetchall()
oldValues=[elem for elem in records[0]]
time=self.addMin(oldValues[-1])
del oldValues[-1]
newValues=[self.randomIncreaseDecrease(elem) for elem in oldValues]
query_delete=f"DELETE FROM temp_minutes LIMIT 1"
mycursor.execute(query_delete,)
self.connection.commit()
mycursor.close()
#here is one part----------------------------------
mycursor = self.connection.cursor()
query_alldata=f"""
INSERT INTO alldata ({self.roomSelect(self.roomNames)}, time)
VALUES {*newValues,str(time)};"""
mycursor.execute(query_alldata,)
self.connection.commit()
#here is the second part----------------------------
mycursor = self.connection.cursor()
query_temp_minutes=f"""
INSERT INTO temp_minutes ({self.roomSelect(self.roomNames)}, time)
VALUES {*newValues,str(time)};"""
mycursor.execute(query_temp_minutes,)
self.connection.commit()
mycursor.close()
self.lock.release()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)