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

MySQL批量插入报错Failed processing format-parameters:Python列表参数问题

解决「Failed processing format-parameters; Python 'list'」报错问题

这个报错的核心原因很明确:你给MySQL传递了Python列表类型的参数,但MySQL Connector无法直接将列表映射为SQL支持的数据类型。咱们一步步排查并修复问题:

1. 最直接的问题:origin被定义为列表

你代码里写了origin = ["Launceston"],这是一个包含单个字符串的列表,但数据库的origin字段应该是字符串类型。把它改成普通字符串即可:

origin = "Launceston"  # 去掉方括号,直接用字符串

2. 隐藏的参数顺序错误

仔细对比你的SQL插入字段和val里的参数顺序:

  • SQL字段顺序最后两位:gate, status
  • 你的val最后两位:status, gate

这会导致参数和字段不匹配,即使前面的问题修复了,后续也可能出现数据错位的问题。修正val的顺序:

val = (origin, flight_id, airline, " ".join(destination), ", ".join(flightNumbers), 
       scheduledTime, estimatedTime, scheduledDate, latestTime, gate, status)
# 把最后两个参数的顺序从 status, gate 改成 gate, status

3. 额外的检查建议

虽然你用" ".join(destination)和", ".join(flightNumbers)处理了列表类型的字段,但要确保这两个字段在数据库里是字符串类型(比如VARCHAR),避免后续出现类型不兼容的问题。

修正后的完整循环代码片段

for element in response_data['departures']:
    origin = "Launceston"  # 修正为字符串
    flight_id = element['AirLineID']
    airline = element['AirLineName']
    destination = element['Route1']
    flightNumbers = element['FlightID']
    scheduledTime = element['Scheduled']
    estimatedTime = element['Estimated']
    scheduledDate = element['DateFormatted']
    latestTime = element['Estimated']
    status = element['Status']
    gate = element['Gate']

    print(origin, flight_id, flightNumbers, airline, destination, scheduledTime, scheduledDate, latestTime, gate, status)

    sql = "INSERT INTO flightinfo (origin, id, airline, destinations, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, gate, status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    val = (origin, flight_id, airline, " ".join(destination), ", ".join(flightNumbers), 
           scheduledTime, estimatedTime, scheduledDate, latestTime, gate, status)  # 修正参数顺序
    data.append(val)

这样修改后,应该就能解决报错,顺利完成批量插入了。

内容的提问来源于stack exchange,提问作者Adam Wolarczuk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:41:10