mysql - 我只将一条记录插入到 mysql 数据库中

标签 mysql python-3.x

我已经让我的代码正常工作,但由于某种原因,当我打印数据时,我只将一条记录插入数据库,我看到了所有正确的数据,但它只是没有进入数据库。我尝试更改代码的不同部分,但没有任何区别,因此不确定我做错了什么,提前谢谢您

from requests_html import HTMLSession
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="**",
  database="flightdata"
)

mycursor = mydb.cursor()

# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get("https://www.adelaideairport.com.au/flight-information/flight-search/?flt_no=&carrier=All&city=&dte=Current&leg=Departures")

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


airline_spans = resp.html.find('.SearchResultFlightListRow')
print (airline_spans)
airline_list = [span.text.split('\n') for span in airline_spans]

for flight in airline_list:
    if len(flight) == 7:
        flightno, From, to, scheduled, estimated, gate, status = flight
        print ("This is a " + estimated)
        if estimated == "":
            print (" currently no dely ")
            print ("This is a " + estimated)
            estimated = 'IDEL'
        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} and flight status is {status}')

    elif len(flight) == 6:
        flightno, From, to, scheduled, estimated, gate = flight
        status = 'IDEL'
        print ("This is a " + estimated)
        if estimated == "":
            print (" currently no dely ")
            print ("This is a " + estimated)
            estimated = 'IDEL'
        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} ')

    elif len(flight) == 5:
        flightno, From, to, scheduled, estimated = flight
        gate = 'IDEL'
        status = 'IDEL'
        print ("This is a " + estimated)
        if estimated == "":
            print (" currently no dely ")
            print ("This is a " + estimated)
            estimated = 'IDEL'

print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate ')


sql = "INSERT INTO flightinfo (origin, airline, destinations, flightNumbers, scheduledTime, estimatedTime, status) VALUES (str(From), str(to), str(flightno), str(scheduled), str(estimated), str(status), str(gate)"

val = (str(From), str(to), str(flightno), str(scheduled), str(estimated), str(status), str(gate))
#data.append(val)
print (val)


# doing a batch insert
#mycursor.executemany(sql, val)
mycursor.executemany(sql,())
mydb.commit()

print(mycursor.rowcount, "was inserted.")

最佳答案

使这项工作有效的最小修改如下:

from requests_html import HTMLSession
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="**",
  database="flightdata"
)

mycursor = mydb.cursor()

# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get("https://www.adelaideairport.com.au/flight-information/flight-search/?flt_no=&carrier=All&city=&dte=Current&leg=Departures")

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


airline_spans = resp.html.find('.SearchResultFlightListRow')
print (airline_spans)
airline_list = [span.text.split('\n') for span in airline_spans]
data = []    

for flight in airline_list:
    if len(flight) == 7:
        flightno, From, to, scheduled, estimated, gate, status = flight
        print ("This is a " + estimated)
        if estimated == "":
            print (" currently no dely ")
            print ("This is a " + estimated)
            estimated = 'IDEL'
        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} and flight status is {status}')

    elif len(flight) == 6:
        flightno, From, to, scheduled, estimated, gate = flight
        status = 'IDEL'
        print ("This is a " + estimated)
        if estimated == "":
            print (" currently no dely ")
            print ("This is a " + estimated)
            estimated = 'IDEL'
        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate {gate} ')

    elif len(flight) == 5:
        flightno, From, to, scheduled, estimated = flight
        gate = 'IDEL'
        status = 'IDEL'
        print ("This is a " + estimated)
        if estimated == "":
            print (" currently no dely ")
            print ("This is a " + estimated)
            estimated = 'IDEL'

        print (f'Flight no {flightno} from  {From} to {to} is scheduled to depart at {scheduled} from gate ')

    val = (str(From), str(to), str(flightno), str(scheduled), str(estimated), str(status), str(gate))
    data.append(val)

sql = "INSERT INTO flightinfo (origin, airline, destinations, flightNumbers, scheduledTime, estimatedTime, status) VALUES (%s, %s, %s, %s, %s, %s, %s)"

# doing a batch insert
mycursor.executemany(sql, data)
mydb.commit()

print(mycursor.rowcount, "was inserted.")

关于mysql - 我只将一条记录插入到 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59112295/

相关文章:

python - 在 PyQt 应用程序上使用 cx_freeze 时出现语法错误

python-3.x - Python 3,初学者错误

mysql - 如何在 MySQL 中每 X 个月返回日期 = 现在的记录?

mysql - 为什么这个 SQL 连接可以按 1 个顺序工作,而另一个则不行?

php - MySQL 错误的时区

php - 如何从列略有不同的两个表中选择行?我怎样才能抵消它们?

Mysql集合代数运算

python-3.x - 如何在python 3中按本地语言对拉丁语进行排序?

python - Pandas 中多索引的百分比

python - 为什么 scipy.optimize 中的 linear_sum_assignment 永远不会返回,如果其中一项分配的成本必须为无穷大?