我需要运行一个消耗JSON数据的小脚本,将其处理成SQL INSERT以附加到现有表中。
这就是我现在所拥有的:
#!/usr/bin/env python
import sys
import time
import json
import string
import datetime
cursor = sqldb.cursor()
json_data = open('/home/user/pythonstuff/test.json')
data = json.load(json_data)
table_name = data['name']
datetime_string = data['timestamp']
print(data)
print(table_name)
print(datetime_string)
fields=[]
for i in data:
fields.append(i[:])
print (fields)
query = "INSERT INTO %s VALUES ('%s',%s,%s,%s...)" % (table_name + 'b',
for i in fields?)
cursor.execute(query)
self.sqldb.commit()
cursor.close()
示例test.json文件:
{"name": "prototype_1", "timestamp": "2017-11-15 18:28:49.741879", "data1": 48059, "data2": 0, "data3": 0, "switch": true}
我想将这些数据映射到具有相同字段(名称,时间戳等)的mysql表中