簡體   English   中英

使用 MySQL 插入命令運行 python 得到參數錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM