python - 使用 MySQL insert 命令运行 python 时出现参数错误

标签 python mysql

我正在尝试执行从 Python 到 MySQL 的插入操作,我得到了

mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement;

我在网上看了看,我知道它与 tuble 有关,但我不知道要更改什么,我环顾四周并查看了我的代码,那里responce_data 中有 10 个项目,SQL 中有 10 个项目,我有 10%s,所以我不知道哪里出错了

 import urllib.parse
import requests
import mysql.connector


mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="**",
  database="flightdata"
)

mycursor = mydb.cursor()


main_api = 'https://www.sydneyairport.com.au/_a/flights/?query=&flightType=departure&terminalType=domestic&date=2019-11-10&sortColumn=scheduled_time&ascending=true&showAll=true'

address = 'lhr'
url = main_api + urllib.parse.urlencode({address: address})

response_data = requests.get(url).json()
for element in response_data['flightData']:
    flight_id = element['id']
    airline = element['airline']
    destination = element['destinations']
    flightNumbers = element['flightNumbers']
    scheduledTime = element['scheduledTime']
    estimatedTime = element['estimatedTime']
    scheduledDate = element['scheduledDate']
    latestTime = element['latestTime']
    status = element['status']
    statusColor = element['statusColor']

    sql = "INSERT INTO flightinfo (id, airline, destinations, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, status, statusColor) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

    #sql = "INSERT INTO flightinfo (flight_id, airline, destination, flightNumbers, scheduledTime, estimatedTime, estimatedTime, scheduledDate, latestTime, status, statusColor ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s ,%s))"
    val = [(flight_id, airline, " ".join(destination), ", ".join(flightNumbers), scheduledTime, estimatedTime,
            scheduledDate, latestTime, status, statusColor)]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")


print(airline, destination, flightNumbers, scheduledTime, "Estimated Time:" + " " + estimatedTime, "Scheduled Date:" + " " + scheduledDate, "Latest Time:" + " " + latestTime, "Status:" + " " +status, "Status Color:" + " " + statusColor)

最佳答案

删除 val 中的尾随 , 并将 val 更改为

val = [(flight_id, airline, destination, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, status, statusColor)]

并且不要忘记将您的sql修复为

sql = "INSERT INTO flightinfo (flight_id, airline, destination, flightNumbers, scheduledTime, estimatedTime, scheduledDate, latestTime, status, statusColor) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

更新: 并避免得到

TypeError: Python 'list' cannot be converted to a MySQL type

您可以根据自己的情况将相关字段转换为字符串。

val = [(flight_id, airline, " ".join(destination), ", ".join(flightNumbers), scheduledTime, estimatedTime, scheduledDate, latestTime, status, statusColor)]

关于python - 使用 MySQL insert 命令运行 python 时出现参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58888107/

相关文章:

python - 广播 numpy 点积

python - 如何使用 asyncio 复杂地管理 shell 进程?

python - 如何减少 python 中的多处理时间

mysql - 按 ASC 排序返回奇怪的结果

javascript - 将单个 mysql 值存储到 javascript 变量中

python - Pandas - `loc` 在除一列之外的所有列上返回空 DataFrame

Python 使用列值对数据框行进行子集化

php - 如何使用 foreach 循环根据所选选项将数据检索到文本区域?

mysql - 我的表在性能方面的最佳结构 :

带有子查询的 MySQL View