2

I am trying to do an insert from Python to MySQL and I am getting

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

I have had a bit of a look online and I know its got something to do with tuble but I can't work out what to change, I have looked around and at my code, there are 10 items in the responce_data, 10 in the SQL and I have 10 %s so I don't know where I am going wrong

 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)
2
  • You sql is wrong it has 11 table columns instead of 10 because of duplicate entry of estimatedTime Commented Nov 16, 2019 at 6:43
  • i took that our and i still have the same issue mate Commented Nov 16, 2019 at 6:55

1 Answer 1

1

Remove the trailing , in val and change val to be

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

And don't forget to fix your sql to

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)"

UPDATE: And to avoid getting

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

You can convert the relevant fields to string for instance in your case you can do.

val = [(flight_id, airline, " ".join(destination), ", ".join(flightNumbers), scheduledTime, estimatedTime, scheduledDate, latestTime, status, statusColor)]
Sign up to request clarification or add additional context in comments.

7 Comments

with new code i am getting File "Sydneyimport.py", line 40, in <module> mycursor.executemany(sql, response_data) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\mysql\connector\cursor.py", line 668, in executemany stmt = self._batch_insert(operation, seq_params) File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\mysql\connector\cursor.py", line 613, in _batch_insert raise errors.ProgrammingError( mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement new code in question
check the update and don't forget to mark as best if solved your problem.
its kida worked but only inserted one row of data when there is around 500 for it to entered, changes in code codeshare.io/alWVLd so you can edit if you want
Thank you mate that worked thank so much , one last question , i need to rerun the code every 5 mins, is there a way to run the query to update all the data that is there and not delete it, i will do another one to delete all data after midnight
also mate i ran the code for today and i only fgot 3 records back and i should be seeing 300+ has that got something to do with address = '1hr'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.