parent
3f9776c4b1
commit
663de7af66
@ -0,0 +1,59 @@
|
|||||||
|
import pandas as pd
|
||||||
|
import mysql.connector
|
||||||
|
from mysql.connector import Error
|
||||||
|
|
||||||
|
# 读取CSV文件
|
||||||
|
df = pd.read_csv('D:\\college\\SE2\\Ctrip-Crawler-main\\Ctrip-Crawler-main\\2024-10-21\\2024-10-20\\天津-贵阳.csv')
|
||||||
|
|
||||||
|
# 数据库连接配置
|
||||||
|
db_config = {
|
||||||
|
'host': 'localhost', # 修改这里,去掉端口号
|
||||||
|
'port': 3307, # 单独指定端口号
|
||||||
|
'database': 'fly_ticket',
|
||||||
|
'user': 'root',
|
||||||
|
'password': '123456'
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 连接到数据库
|
||||||
|
conn = mysql.connector.connect(**db_config)
|
||||||
|
|
||||||
|
if conn.is_connected():
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# 为每行数据创建插入语句
|
||||||
|
for index, row in df.iterrows():
|
||||||
|
sql = """INSERT INTO flight (f_n, f_s_p, f_a_p, f_s_a, f_a_a, f_s_t, f_a_t, f_Date, f_Delay, f_p, f_food, f_wide, f_depcode, f_dstcode)
|
||||||
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
|
||||||
|
|
||||||
|
values = (
|
||||||
|
row['航班号'],
|
||||||
|
row['出发城市'],
|
||||||
|
row['到达城市'],
|
||||||
|
row['出发机场'],
|
||||||
|
row['到达机场'],
|
||||||
|
row['出发时间'],
|
||||||
|
row['到达时间'],
|
||||||
|
row['出发日期'],
|
||||||
|
row['出发延误时间'],
|
||||||
|
row['economy_origin'],
|
||||||
|
row['经济舱餐食信息'],
|
||||||
|
row['经济舱座椅间距'],
|
||||||
|
row['出发机场三字码'],
|
||||||
|
row['到达机场三字码']
|
||||||
|
)
|
||||||
|
|
||||||
|
cursor.execute(sql, values)
|
||||||
|
|
||||||
|
# 提交更改
|
||||||
|
conn.commit()
|
||||||
|
print("数据成功插入到数据库")
|
||||||
|
|
||||||
|
except Error as e:
|
||||||
|
print(f"连接数据库时出错: {e}")
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if 'conn' in locals() and conn.is_connected():
|
||||||
|
cursor.close()
|
||||||
|
conn.close()
|
||||||
|
print("数据库连接已关闭")
|
Loading…
Reference in new issue