gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:38:33 24 4
gpt4 key购买 nike

我正在尝试执行从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com