命令不同步;您可以立即运行此命令,同时尝试通过某种条件从表中删除行

发布于 2025-01-27 10:34:15 字数 472 浏览 1 评论 0原文

我试图根据某个条件从数据库中的表中删除行(在这种情况下为特定日期) 此查询在MySQL Workbench中没有任何问题,但是通过Python,我得到了此错误: “命令不同步;您现在无法运行此命令”

def delete_from_available_donations (self):
  self.cursor.execute("SET SQL_SAFE_UPDATES = 0;\
                       DELETE FROM available_donations WHERE expiry_date = '2022-05-27'")
  res = self.connection.commit()
  return  1    

def main():
  q = DataQueries("root", "amitbrilant" , "Blood_bank") 
  q.delete_from_available_donations()

I'm trying to delete rows from a table in the database according to a certain condition (in this case by a specific date)
This query works without any problem in MySQL workbench, but through Python, I get this error:
"Commands out of sync; you can not run this command now"

def delete_from_available_donations (self):
  self.cursor.execute("SET SQL_SAFE_UPDATES = 0;\
                       DELETE FROM available_donations WHERE expiry_date = '2022-05-27'")
  res = self.connection.commit()
  return  1    

def main():
  q = DataQueries("root", "amitbrilant" , "Blood_bank") 
  q.delete_from_available_donations()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2025-02-03 10:34:16

如已经讨论的在这里当您搜索错误消息时,您必须先在提交之前先从光标取得结果。

这也意味着您必须将语句分为单个执行,以便能够在每个语句之后释放光标。

根据我的经验,可以拆分语句并捕获结果生成器(fetchall不需要)

# ...
res = self.cursor.execute("YOUR SINGLE STATEMENT")
res = self.cursor.execute("YOUR SINGLE STATEMENT")
# ...
self.connection.commit()
# ...

这是mysql-connectorpymysql >。老实说,我不知道为什么...

As already discussed here when you search your error message, you have to fetch results from the cursor first before you commit.

This also means that you have to split your statements into single executes to be able to free the cursor after each statement.

From my experience it is sufficient to split the statements and catch the result generator (fetchall is not necessary)

# ...
res = self.cursor.execute("YOUR SINGLE STATEMENT")
res = self.cursor.execute("YOUR SINGLE STATEMENT")
# ...
self.connection.commit()
# ...

This is diffrent for mysql-connector and pymysql. To be honest I don't know why...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文