You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python操作MySQL执行INSERT语句报Could not process parameters错误如何解决

错误产生原因
  • executemany()方法使用不符合规范。该方法用于批量执行多条同结构SQL,要求第二个参数必须是包含多组参数的可迭代对象(如列表套元组,每个元组对应一条SQL的完整参数),你当前传入的是单组参数的元组,方法解析参数时会遍历单个元组的每个元素作为独立参数组,匹配不上SQL的3个占位符,触发参数解析错误。
  • MySQL Python连接器的参数占位符统一要求使用%s,不支持%d这类带类型限定的占位符,即使是数值类型也要用%s,连接器会自动完成数据类型转换。
修正方案

根据你的使用场景二选一即可:

场景1:仅插入单条数据

将executemany替换为单条执行的execute方法,同时修改占位符格式:

my_cursor = my_db.cursor()

def Insert_object(Title, Details, Amount):
    # 所有占位符统一用%s,无需指定类型
    sql_command = 'INSERT INTO income (title, details, amount) VALUES (%s, %s, %s)'
    values = (Title, Details, Amount)
    # 单条插入用execute方法
    my_cursor.execute(sql_command, values)
    my_db.commit()


Insert_object('Chips', 'Chili', 12)

场景2:需要批量插入多条数据

保留executemany,调整入参格式为多组参数组成的列表:

my_cursor = my_db.cursor()

def Insert_batch_objects(batch_values):
    sql_command = 'INSERT INTO income (title, details, amount) VALUES (%s, %s, %s)'
    my_cursor.executemany(sql_command, batch_values)
    my_db.commit()


# 入参为多组数据组成的列表
Insert_batch_objects([
    ('Chips', 'Chili', 12),
    ('Coke', 'Zero Sugar', 8),
    ('Bread', 'Whole Wheat', 15)
])

内容的提问来源于stack exchange,提问作者Amirali Sinaee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 23:45:00