【问题标题】:Mysql insert with Python Could not Process ParametersMysql insert with Python 无法处理参数
【发布时间】:2022-12-11 00:54:39
【问题描述】:

我正在研究 python 中的一个函数,它从 MySql 中读取值。当我使用相同的格式插入时没有错误,但是当我选择它时却出现错误 mysql.connector.errors.ProgrammingError:无法处理参数:str(555), 它必须是列表、元组或字典类型 这是我给出错误的代码

cursor.execute('SELECT * FROM LIBS')
            result = cursor.fetchall()
            for row in result:
                user_id = row[0]
                song_id = row[1]
                if(user_id == userno):
                    cursor.execute("SELECT s_title FROM SONG WHERE s_id = (%s)", (song_id))
                    print(row[1])

我不明白的是,当我使用完全相同的格式插入 (%s) 时,没有错误

我尝试在 song_id 中添加一个逗号并声明一个 sql 语句和一个值,然后将它们作为 cursor.execute(sql, (value,)) 运行,但我收到错误 mysql.connector.errors.InternalError: Unread result found。

【问题讨论】:

    标签: python mysql sql


    【解决方案1】:

    params 必须是列表、元组或字典(参见docs

    cursor.execute("SELECT s_title FROM SONG WHERE s_id = (%s)", (song_id,))
    

    注意song_id 后面的逗号。没有逗号,它只是一个 str 而不是 tuple。这仅在创建具有一个元素的元组时是必需的。

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 2019-08-06
      • 2021-10-04
      • 2019-06-28
      • 2020-04-14
      • 1970-01-01
      • 2013-08-02
      • 2017-11-25
      相关资源
      最近更新 更多