[英]Running a python with MySQL insert command getting an error on parameters
我正在尝试从Python到 MySQL 插入,我得到了
mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement;
我在网上看了一些,我知道它与tuble
,但我不知道要更改什么,我环顾四周,在我的代码中, responce_data
中有10 个项目,在SQL 我有 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)
删除尾随,
in 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' 无法转换为 MySQL 类型
例如,您可以将相关字段转换为字符串。
val = [(flight_id, airline, " ".join(destination), ", ".join(flightNumbers), scheduledTime, estimatedTime, scheduledDate, latestTime, status, statusColor)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.