-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathjsonToMySQL.py
More file actions
46 lines (35 loc) · 920 Bytes
/
jsonToMySQL.py
File metadata and controls
46 lines (35 loc) · 920 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
import sys
# reload(sys)
# sys.setdefaultencoding('utf8')
import json
import codecs
import MySQLdb
def read():
json_file = codecs.open("items.json", 'r', 'utf-8')
data = json.load(json_file)
return data
def save(index_,url,description,date,title):
try:
db = MySQLdb.connect("localhost","root","666666","python",charset='utf8')
data = (index_,url,description,date,title)
cursor = db.cursor()
except:
print ("error")
sql = "INSERT INTO `search_result` (`index_`, `url`, `description`, `date`, `title`) VALUES(%s,%s,%s,%s,%s)"
try:
cursor.execute(sql,data)
db.commit()
except:
db.rollback()
cursor.close()
db.close()
data = read()
global index
index = 400
for line in data:
#print line['description'][0]
if(line['description'] and line['title']):
save(index,line['url'],line['description'][0],'2017-04-15',line['title'][0])
index +=1
#print index