Inserting double dictionary into MYSQL with PYTHON

Viewed 25

I cant figure any way to do this. I have this dictionary from an API request and I am trying to store it into a database. This is the dictionary

    descr = {
                        "id": "order-1",
                        "location": {
                        "latitude": location.latitude,
                        "longitude": location.longitude

                    },
                    "serviceDuration": 400,
                    "quantity": 2
                }

then i tried to set each item i need to a variable (which doesnt set some reason but print(descr["id"]) would print)

    id_api = str(descr["id"])
    location_latitude = str(descr["location"]["latitiude"])
    location_longitude = str(descr["location"]["longitude"])
    serviceDuration = str(descr["serviceDuration"])
    quantity = str(descr["quantity"])

then im trying to insert into database which works for me with regular variables but doesnt work with this dictionary or how im doing now, because of above issue with setting variable to dictionary item

    mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        password="",
        database="thedb"
        )

    sql = "INSERT INTO customers (chatid, id_api,location_latitude,location_longitude,serviceDuration,quantity) VALUES (%s,%s,%s,%s,%s,%s)"

    val = (str(chat_id),str(id_api),str(location_latitude),str(location_longitude),str(serviceDuration),str(quantity))
    mycursor.execute(sql, val)

    mydb.commit()
0 Answers
Related