简体   繁体   中英

insert multiple rows from JSON object in MySQL with Python

I'm trying to add multiple rows from a JSON object into MySQL but only the last row is added to the database. Is there a solution like "for each row"?

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()

The JSON object looks like this and can hold between 5 and 50 records at once

[{"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"}]

Error in indent in cursor.execute. Here is fixed code

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()

You should put your INSERT statement inside the for loop:

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"]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM