python - MySQL 命令与 Python 不同步

标签 python mysql sql

我正在尝试使用 MySQL 和 python mysql 连接器初始化我的数据库。 运行下面的代码后,我收到此异常:

mysql.connector.errors.DatabaseError: 2014 (HY000): Commands out of sync; you can't run this command now

我尝试了几件事,包括提交连接对象,但对我来说没有任何效果。

   def initialization():
        cnx = mysql.connector.connect(user="root", password="xxxxxxxxx", host='127.0.0.1')
        cursor = cnx.cursor()
        cursor.execute("CREATE DATABASE IF NOT EXISTS izugitdb;")
        cursor.execute("USE izugitdb;")
        cursor.execute("CREATE TABLE IF NOT EXISTS employee_users (user_id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(40) NOT NULL, password VARCHAR(40) NOT NULL,isadmin TINYINT(1) DEFAULT 0 NOT NULL);")
        cursor.execute("CREATE TABLE IF NOT EXISTS clients (client_id INT AUTO_INCREMENT PRIMARY KEY,client_name VARCHAR(255) NOT NULL); ENGINE = InnoDB;")
        cursor.execute("CREATE TABLE IF NOT EXISTS clients_workers (id INT AUTO_INCREMENT PRIMARY KEY, worker_id INT ,worker_name VARCHAR(255) NOT NULL, client_id INT);")

最佳答案

Commands out of sync

If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.

This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.

也许,您的案例中的命令,您是否尝试过直接在 MySQL 中使用它们?并能够重现错误?

或者尝试在每个命令后关闭游标,然后在使用它执行另一条语句之前再次打开它:

cursor.close() 

cursor = cnx.cursor() 

附言。 @Nikos Steiakakis 说得好,我已经编辑并隐藏了你的密码,但愿这不是一个真实的密码。

关于python - MySQL 命令与 Python 不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53994303/

相关文章:

sql - DoCmd.OpenQuery 或 DoCmd.RunSQL

python - 为什么 `if False`后面的导入会生成局部变量?

python - Keras Lambda CTC 无法加载模型

mysql - 计算特定行 mysql + express

mysql concat 从选择结果

mysql - 使用 != 而不是 < 时得到错误结果

python - 合并时间戳大于或等于的数据帧

python - 使用 cookie 从 Google Scholar(bibtex) 导入数据

mysql - 通过 mysql 删除 mediawiki 垃圾邮件用户

java - 插入语句的Postgresql错误代码