使用Python从MySQL中的JSON对象插入多行
我试图将JSON对象中的多行添加到MySQL中,但只将最后一行添加到数据库中。有没有像“每行对应”这样的解决方案 JSON对象看起来像这样,一次可以保存5到50条记录使用Python从MySQL中的JSON对象插入多行,python,mysql,python-3.x,mysql-python,Python,Mysql,Python 3.x,Mysql Python,我试图将JSON对象中的多行添加到MySQL中,但只将最后一行添加到数据库中。有没有像“每行对应”这样的解决方案 JSON对象看起来像这样,一次可以保存5到50条记录 [{"one":"1.232.123","two":"test","three":1242,"four":"2,4","five":"test","six":"test","seven":"test"},{"one":"3.322.876","two":"test","three":1312,"four":"3,4","five"
[{"one":"1.232.123","two":"test","three":1242,"four":"2,4","five":"test","six":"test","seven":"test"},{"one":"3.322.876","two":"test","three":1312,"four":"3,4","five":"test","six":"test","seven":"test"},{"one":"1.232.123","two":"test","three":1242,"four":"2,4","five":"test","six":"test","seven":"test"},{"one":"3.322.876","two":"test","three":1312,"four":"3,4","five":"test","six":"test","seven":"test"}]
您应该将
INSERT
语句放入for
循环中:
for obj in json_obj:
print(obj["one"])
print(obj["two"])
print(obj["three"])
print(obj["four"])
print(obj["five"])
print(obj["six"])
print(obj["seven"])
cursor.execute("INSERT INTO test (one, two, three, four, five, six, seven) VALUES (%s,%s,%s,%s,%s,%s,%s)", (obj["one"], obj["two"], obj["three"], obj["four"], obj["five"], obj["six"], obj["seven"]))
您应该将
INSERT
语句放入for
循环中:
for obj in json_obj:
print(obj["one"])
print(obj["two"])
print(obj["three"])
print(obj["four"])
print(obj["five"])
print(obj["six"])
print(obj["seven"])
cursor.execute("INSERT INTO test (one, two, three, four, five, six, seven) VALUES (%s,%s,%s,%s,%s,%s,%s)", (obj["one"], obj["two"], obj["three"], obj["four"], obj["five"], obj["six"], obj["seven"]))
cursor.execute中的缩进错误。 这是固定代码
import requests
import json
import pymysql
import urllib.request
con = pymysql.connect(host = 'host', port = 3306, user = 'user', passwd = 'pass', db = 'db')
cursor = con.cursor()
url = 'url to json'
urllib.request.urlopen(url).read()
response = urllib.request.urlopen(url).read()
json_obj = str(response, 'utf-8')
json_obj = json.loads(response.decode('utf-8'))
for obj in json_obj:
print(obj["one"])
print(obj["two"])
print(obj["three"])
print(obj["four"])
print(obj["five"])
print(obj["six"])
print(obj["seven"])
cursor.execute("INSERT INTO test (one, two, three, four, five, six, seven) VALUES (%s,%s,%s,%s,%s,%s,%s)", (obj["one"], obj["two"], obj["three"], obj["four"], obj["five"], obj["six"], obj["seven"]))
con.commit()
con.close()
cursor.execute中的缩进错误。 这是固定代码
import requests
import json
import pymysql
import urllib.request
con = pymysql.connect(host = 'host', port = 3306, user = 'user', passwd = 'pass', db = 'db')
cursor = con.cursor()
url = 'url to json'
urllib.request.urlopen(url).read()
response = urllib.request.urlopen(url).read()
json_obj = str(response, 'utf-8')
json_obj = json.loads(response.decode('utf-8'))
for obj in json_obj:
print(obj["one"])
print(obj["two"])
print(obj["three"])
print(obj["four"])
print(obj["five"])
print(obj["six"])
print(obj["seven"])
cursor.execute("INSERT INTO test (one, two, three, four, five, six, seven) VALUES (%s,%s,%s,%s,%s,%s,%s)", (obj["one"], obj["two"], obj["three"], obj["four"], obj["five"], obj["six"], obj["seven"]))
con.commit()
con.close()