【问题标题】:Running a python with MySQL insert command getting an error on parameters使用 MySQL 插入命令运行 python 获取参数错误
【发布时间】:2019-11-16 06:11:27
【问题描述】:

我正在尝试从 Python 向 MySQL 进行插入,我得到了

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

我在网上看了一些,我知道它与 tuble 有关,但我不知道要更改什么,我环顾四周并查看我的代码,有 10 responce_data 中的项目,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)

【问题讨论】:

  • 你的 sql 是错误的,它有 11 table columns 而不是 10,因为 estimatedTime 的重复条目
  • 我认为我们和我仍然有同样的问题 mate

标签: python mysql


【解决方案1】:

去掉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)]

【讨论】:

  • 使用新代码,我在 mycursor.executemany(sql, response_data) 文件“C:\Users\User\AppData\Local\Programs 中获取文件“Sydneyimport.py”,第 40 行\Python\Python38\lib\site-packages\mysql\connector\cursor.py”,第 668 行,在 executemany stmt = self._batch_insert(operation, seq_params) 文件“C:\Users\User\AppData\Local\Programs\ Python\Python38\lib\site-packages\mysql\connector\cursor.py",第 613 行,在 _batch_insert 中引发错误。ProgrammingError(mysql.connector.errors.ProgrammingError:并非所有参数都在 SQL 语句中使用了新代码
  • 检查更新,如果解决了您的问题,别忘了标记为最佳。
  • 它的 kida 工作但只插入了一行数据,当有大约 500 条数据可供输入时,更改代码 codeshare.io/alWVLd 以便您可以根据需要进行编辑
  • 谢谢你的工作,非常感谢,最后一个问题,我需要每 5 分钟重新运行一次代码,有没有办法运行查询来更新所有存在的数据而不是删除它,我会在午夜后再次删除所有数据
  • 另外,我运行了今天的代码,我只返回了 3 条记录,我应该看到 300+ 与 address = '1hr' 有关系
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 2016-11-23
  • 2016-05-02
  • 1970-01-01
  • 2012-05-26
相关资源
最近更新 更多