_mysql_connector.MySQLInterfaceError: Commands out of sync; you can't run this command now python msql.connector

_mysql_connector.MySQLInterfaceError: Commands out of sync; you can't run this command now python msql.connector

我有一个功能,您可以在下面看到。如果 运行 这个函数我会得到你在标题中看到的错误,你能帮我吗?

不久前我能够用锁解决这个问题,但现在它们不起作用。我知道这与我的连接有关,但我不知道如何解决这个问题

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()