码迷,mamicode.com
首页 > 数据库 > 详细

json通过pymysql写入mysql

时间:2017-10-22 00:31:45      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:通过   creat   blog   title   val   utf-8   use   col   pymysql   

 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

标签:通过   creat   blog   title   val   utf-8   use   col   pymysql   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!