Python Forum
error when inserting list statement from python to MySQL
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error when inserting list statement from python to MySQL
#1
HI all i am trying to insert data from a python script i have and i am getting an error, i thank you for your help in advance and feel free to let me know if there is a better way to do what I am trying to do here , as i am still new to coding with python any help would be good, i will be adding other locations soon as please letme know if that would be best to do each with another file or all in the one
import mysql.connector
from requests_html import HTMLSession


# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get("https://www.sydneyairport.com.au/flights/?query=&flightType=departure&terminalType=domestic&date=2019-11-05&sortColumn=scheduled_time&ascending=true&showAll=false")

# Run JavaScript code on webpage
resp.html.render()

# parse <span class="with-image"> elements containing airline names
airline_list = []
airline_spans = resp.html.find('.with-image')
for span in airline_spans:
    airline_list.append(span.text)

# parse <div class="city-name"> elements containing airline names
dest_list = []
dest_divs = resp.html.find('.city-name')
for span in dest_divs:
    dest_list.append(span.text)

# parse <div class="flight-numbers"> elements containing Flight numbers
flightnumber_list = []
flightnumber_divs = resp.html.find('.flight-numbers')
for span in flightnumber_divs:
    flightnumber_list.append(span.text)


# parse <div class="heading-large"> elements containing Sec depart time
secdepartime_list = []
secdepartime_divs = resp.html.find('.large-scheduled-time')
for span in secdepartime_divs:
    secdepartime_list.append(span.text)


# parse <div class="estimated-time"> elements containing estimated-time
estimatedtime_list = []
estimatedtime_divs = resp.html.find('.estimated-time')
for span in estimatedtime_divs:
    estimatedtime_list.append(span.text)

# parse <div class="estimated-time"> elements containing latesttime
status_list = []
status_divs = resp.html.find('.status-container')
for span in status_divs:
    status_list.append(span.text)


#Connecting go the mysql database to store the infomation

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="*******",
  database="FlightData"
)
print("Adding Sydney Domestic Airport Flight Information to the Database")
mycursor = mydb.cursor()
sql = "INSERT INTO FlightData (Origan, destination,Flight-Number, Airline, Scheduled,Estimated,Status ) VALUES (%s, %s)"
val = ("Sydney", dest_list, flightnumber_list, airline_list, secdepartime_list, estimatedtime_list, status_list)
mycursor.execute(sql, val)
mydb.commit()
print(airline_list)
print(dest_list)
print(flightnumber_list)
print(secdepartime_list)
print(estimatedtime_list)
print(status_list)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in python and mysql statement vj78 4 915 Apr-08-2021, 02:08 PM
Last Post: vj78
  Python and MYSQL Resultset vj78 2 753 Apr-02-2021, 12:41 AM
Last Post: vj78
  Python/BeautiifulSoup. list of urls ->parse->extract data to csv. getting ERROR IanTheLMT 2 2,041 Jul-04-2019, 02:31 AM
Last Post: IanTheLMT
  mysql database error brecht83 1 3,620 Dec-14-2018, 01:25 PM
Last Post: jeanMichelBain
  Inserting multiple cookies using python requests freshtomatoes 0 3,348 Aug-16-2018, 09:25 PM
Last Post: freshtomatoes
  Q. Nested for statement in list type ives 3 1,646 Mar-14-2018, 07:30 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020