我正在开发一个Python项目,在该项目中我使用定义的过程和函数等多次查询数据库。然而,在出现错误之前,我似乎只能从一个准备好的语句中获得结果。
这是代码:
db = mysql.connector.connect(host="localhost", user="root", passwd="root", database="srd")
cursor = db.cursor()
# Column names
cursor.execute("CALL show_columns(\"race\")")
labels = cursor.fetchall()
# concat it into one line
build = ""
for labels in labels:
build = build + labels[0] + " | "
print(build)
# content
cursor.execute("CALL select_all(\"race\")")
results2 = cursor.fetchall()
for result in results2:
print(result)
cursor.close()
这是输出:
race_name | strengthASI | dexterityASI | constitutionASI | wisdomASI | intelligenceASI | charismaASI | flex_points | size | speed | abilities | proficiencies | has_subrace |
// This part is correct output for printing the columns (the first query)
File "C:\Python36\lib\site-packages\mysql\connector\connection_cext.py", line 472, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Commands out of sync; you can't run this command now
mysql.connector.errors.DatabaseError: 2014 (HY000): Commands out of sync; you can't run this command now
最佳答案
阅读文档后,我使用 cursor.execute("CALL ...")
调用过程是错误的。
正确的方法是使用 cursor.callproc()
方法,并通过 cursor.stored_results
检索数据.
关于python - mysqlconnector python错误: "Commands out of sync; you can' t run this command now",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59150003/