-
Notifications
You must be signed in to change notification settings - Fork 0
/
chengyu.py
53 lines (42 loc) · 1.66 KB
/
chengyu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import mysql.connector
def create_table(conn):
cursor = conn.cursor()
try:
cursor.execute('''
CREATE TABLE IF NOT EXISTS idiom_library (
id INT AUTO_INCREMENT PRIMARY KEY,
idiom VARCHAR(255) comment '成语',
pinyin VARCHAR(255) comment '拼音',
remark TEXT comment '解释'
)
''')
print("表格创建成功!")
except mysql.connector.Error as e:
print("创建表格出错:", e)
def insert_data(conn, idiom, pinyin, remark):
cursor = conn.cursor()
try:
cursor.execute('INSERT INTO idiom_library (idiom, pinyin, remark) VALUES (%s, %s, %s)',
(idiom, pinyin, remark))
conn.commit()
print("数据插入成功!")
except mysql.connector.Error as e:
print("数据插入出错:", e)
if __name__ == "__main__":
connect = mysql.connector.connect(
host=os.environ['MYSQL_HOST'],
user="root",
password=os.environ["MYSQL_PASSWORD"],
database="motcs")
create_table(connect) # 创建表格
with open('idiom.txt', 'r', encoding='utf-8') as file:
entries = file.read().split("\n\n") # 假设每条数据之间有一个空行分隔
for entry in entries:
print(entry)
parts = entry.strip().split(",")
idiom_s = parts[0].strip()
pinyin_s = parts[1].split(":")[1].strip()
remark_s = parts[2].split(":")[1].strip()
# 调用函数插入数据
insert_data(connect, idiom_s, pinyin_s, remark_s)