json通过pymysql写入mysql

 1 #coding=utf-8 
 2 
 3 import json
 4 import pymysql
 5 
 6 db = pymysql.connect("localhost","root","asdfg48520.","test",use_unicode=True, charset="utf8")
 7 cursor = db.cursor()
 8 sql = """ DROP TABLE  IF EXISTS SHUJU1 """
 9 cursor.execute(sql)
10 
11 sql = """CREATE TABLE SHUJU1 (
12          TITLE  VARCHAR(20) NOT NULL,
13          TIME  VARCHAR(200),
14          TEXTT VARCHAR(200),
15          URL VARCHAR(200) )"""
16 cursor.execute(sql)
17 db.commit()     
18 data = []
19 
20 with open(r'C:/Users/18341/temp/shixi/10.21/pc1.json') as f:
21     for line in f:
22         data.append(json.loads(line))
23 
24 for item in data:
25     sql="""INSERT INTO SHUJU1 (TITLE,TIME,TEXTT,URL)VALUES(%s,%s,%s,%s)"""
26     cursor.execute(sql,(item['time'],item['url'],item['title'],item['text']))
27 
28 db.commit()
29 db.close()
原文地址:https://www.cnblogs.com/xflqm/p/7706876.html