将json多行数据传入到mysql中使用python实现(简化版)

import json
import pymysql

coon=pymysql.connect(host="192.168.67.41",port=3306,user="root",password="123456",db="weibo");
cursor=coon.cursor();

with open("data.txt","r",encoding="utf-8") as file:
    dates=file.readlines();
    for item in dates:
        t = json.loads(item);
        effect=cursor.execute(
            "insert into weibo_info(id,created_at,content,source,reports_count,comments_count,attitudes_count) values (%s,%s,%s,%s,%s,%s,%s)"
            ,(t["id"],t["created_at"],t["content"],t["source"],
              t["reports_count"],t["comments_count"],t["attitudes_count"]));
        print(effect);
        coon.commit();
cursor.close();
coon.close();

你可能感兴趣的:(将json多行数据传入到mysql中使用python实现(简化版))