how to insert python in to mysql

18
November 08, 2019, at 2:10 PM

I am writing an insert statement for python to add data to a mysql database and I am getting an error

mysql_connector.MySQLInterfaceError: Python type list cannot be converted

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"
)
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)
READ ALSO
Python mysql-connector SELECT in loop returns outdated result

Python mysql-connector SELECT in loop returns outdated result

I'm using mysql-connector to run a SELECT query in a while loop to look for new jobs

18
What is the fastest way to implementing algorithm to manipulate and insert sql records?

What is the fastest way to implementing algorithm to manipulate and insert sql records?

I have a table named as TRENDS, containing around 20k recordsI need to manipulate each row of TRENDS table based on the each column value and final output of the row is a string, named insight which is nothing but that manipulated row

25
Speed up group by

Speed up group by

Consider the following query:

44
Update MySQL root password - Ubuntu [duplicate]

Update MySQL root password - Ubuntu [duplicate]

This question already has an answer here:

41