You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
823 B
37 lines
823 B
# -*- coding: gbk -*-
|
|
from typing import List
|
|
|
|
import mysql
|
|
import mysql.connector
|
|
import csv
|
|
|
|
db_config = {
|
|
'host': 'Mysql',
|
|
'user': 'root',
|
|
'password': '21412030117',
|
|
'database': 'words',
|
|
}
|
|
|
|
cnx = mysql.connector.connect(**db_config)
|
|
cursor = cnx.cursor()
|
|
|
|
# 读取CSV
|
|
with open('extracted_data.csv', mode='r', encoding='utf-8') as file:
|
|
reader = csv.reader(file)
|
|
headers = next(reader, None)
|
|
if headers is not None: # 过表头
|
|
for row in reader:
|
|
sql = "INSERT IGNORE INTO words(xuhao,neirong) VALUES (%s, %s)"
|
|
cursor.execute(sql, (row[0], row[1])) #设csv与数据库对应
|
|
|
|
try:
|
|
# ...(原来的执行代码)
|
|
cnx.commit()
|
|
print("数据导入成功!")
|
|
except mysql.connector.Error as err:
|
|
print(f"数据导入失败: {err}")
|
|
finally:
|
|
cursor.close()
|
|
cnx.close()
|
|
|