【问题标题】:_mysql_connector.MySQLInterfaceError: Commands out of sync; you can't run this command now python msql.connector_mysql_connector.MySQLInterfaceError:命令不同步;你现在不能运行这个命令 python msql.connector
【发布时间】:2021-10-13 00:25:30
【问题描述】:

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

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

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

【问题讨论】:

  • 你已经在python中有一个事务为什么oyu还要添加另一个查询运行

标签: mysql mysql-python mysql-connector


【解决方案1】:

解决方法是不能同时执行 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()

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 2020-04-20
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2013-04-08
    • 2016-04-11
    相关资源
    最近更新 更多