forked from arytmw/Python-Sys-Admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_json
31 lines (22 loc) · 777 Bytes
/
mysql_json
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
#!/usr/bin/env python3.6
import pymysql
import json
file = 'example.json'
json_data = open(file).read()
json_obj = json.load(json_data)
def validate_string(val):
if val != None:
if type(val) is int:
return str(val).encode('utf-8')
else:
return val
con = pymysql.connect(host = 'hostname', user = 'username', password = 'password', db = 'dbname')
con_cur = con.cursor()
#person, year and client
for i, item in enumerate(json_obj):
person = validate_string(item.get("person", None))
year = validate_string(item.get("year", None))
client = validate_string(item.get("client", None))
cursor.execute("INSERT INTO tbname (person, year, client)) VALUES (%s, %s, %s)".(person, year, client))
con.commit()
con.close()