import json
import pymysql
import codecs;
# 连接数据库
conn = pymysql.connect(host='localhost', user='root', password='HelloWorld123', db='water', charset='utf8mb4')
try:
    with conn.cursor() as cursor:
        content = ""
        with codecs.open("dsj.json",encoding="utf-8") as lud:
            content = lud.read()
        # 解析JSON数据
        data = json.loads(content)
        
        # 插入数据到数据库
        for item in data:
            print(item)
            cursor.execute("INSERT INTO Project_milestones (year, date, title, pos, content) VALUES (%s, %s, %s, %s, %s)",
                           (item['year'], item['date'], item['title'], item['pos'], item['content']))
        
        conn.commit()
finally:
    conn.close()