python - mysqlconnector python错误: "Commands out of sync; you can' t run this command now"

标签 python mysql mysql-connector

我正在开发一个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/

相关文章:

php - 将编号列表列添加到返回的 MySQL 查询

python - 如何获取列表列表中的每个元素?

python - pip安装错误: Microsoft Visual C++ 10. 0是必需的

python - 如果 x != 0,则不带 0.x.x.x 的 ipv4 地址的正则表达式

php - MySQL 插入 : Test first?

c# - 如何连接MySQL数据库?

python - 处理同名类、模块和包的 Pythonic 方式是什么?

mysql - SQL——根据生日记录计算年龄

java - 将 isvalid() 函数与 mysql 连接对象一起使用时出现 AbstractMethodError

c++ - 过程在 mysql workbench 中有效,但不适用于 C++ 连接器