首页 > 数据库技术 > 详细

json通过pymysql写入mysql

时间:2017-10-21 23:43:29      阅读:1092      评论:0      收藏:0      [点我收藏+]
 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(rC:/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()

 

json通过pymysql写入mysql

原文:http://www.cnblogs.com/xflqm/p/7706876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!