我正在尝试使用 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);")
最佳答案
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/