json数据格式:
# json格式数据 data={ "camera": { "created": "1531925035", "type": 1, "description": "A bridge is a structure that is built over a railway, river, or road so that people or vehicles can cross from one side to the other.", "location": { "locationID":"1", "country": "china", "city": "xian", "region": "changanqu" }, "project_id": "Golden Gate Bridge", "task_id": "23232", "camera_id": "23232", "mu": "mm", "value": { "x": "123123", "y": "123123" }, "ext_info": "null" } }
import json
import pymysql
def get_data():
with open('./camera.json', 'r') as f:
camera_text = json.load(f) # 解析每一行数据
return camera_text
def data_insert(camera_text):
db = pymysql.connect("localhost", "root", "123456", "filestore")
cursor = db.cursor()
#print(type(camera_text))
value_ca=((camera_text['camera']['created'], camera_text['camera']['type'],
camera_text['camera']['description'],camera_text['camera']['location']['locationID'],
camera_text['camera']['project_id'], camera_text['camera']['task_id'],
camera_text['camera']['camera_id'], camera_text['camera']['mu'],
camera_text['camera']['value']['x'], camera_text['camera']['value']['y'],
camera_text['camera']['ext_info']))
value_lo=((camera_text['camera']['location']['locationID'],
camera_text['camera']['location']['country'],
camera_text['camera']['location']['city'],
camera_text['camera']['location']['region']))
#print(value_lo)
#insert_lo = "insert into location(lidlocation,country,city,region) values (%s,%s,%s,%s)"
insert_ca = "insert into camera(date,type,description,CIDlocation,IDproject,IDtask,IDcamera,mu,value_x,value_y,ext_info)values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
cursor = db.cursor()
#cursor.execute(insert_lo,value_lo)
cursor.execute(insert_ca,value_ca)
db.commit()
cursor.close()
'''except Exception as e:
db.rollback()
print(str(e))
break'''
if __name__ == "__main__": # 起到一个初始化或者调用函数的作用
a=get_data()
data_insert(a)