更新空的JSON字段,其中id =

时间:2018-12-19 21:12:40

标签: python mysql json

我是JSON和MySQL的新手,我需要弄清楚如何将JSON数据插入ID =数字的JSON列中。当前,JSON列中没有数据,但是所有其他列都有数据。

我只是在寻找一种通过命令行插入json的简单语法

import mysql.connector
import json
import os


file = os.path.abspath("C:\mysql\customers.json")
json_data = open(file).read()
json_obj = json.loads(json_data)



mydb = mysql.connector.connect(
    host = "localhost",
    user = "root",
    passwd = "password123",
    database = "testdb",

    )

cursor = mydb.cursor()

for i, item in enumerate(json_obj):
    customerNumber = validate_string(item.get("customerNumber",None))
    customerLastName = validate_string(item.get("customerLastName",None))
    customerFirstName = validate_string(item.get("customerFirstName",None))

    cursor.execute("INSERT INTO json_data (customerNumber, customerLastName, customerFirstName') VALUES (%s, %s, %s)",(customerNumber,customerLastName,customerFirstName))
mydb.commit()
mydb.close()

0 个答案:

没有答案