Skip to content

Instantly share code, notes, and snippets.

@rsudip90 rsudip90/mysql-json-ex.py
Last active Jul 15, 2018

Embed
What would you like to do?
MySQL JSON type data handling in python
import mysql.connector
import json
config = {
"user": "",
"password": "",
"host": "localhost",
"database": "test",
"use_pure": True,
}
json_data = {
"Name": "Harry Potter",
"Email": "harry@earth.world",
"CellPhone": "xxx0000xxx",
"Address": "Earth, Milky Way!!"
}
b_data = json.dumps(json_data)
docID = 1
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor(prepared=True)
# stmt = "UPDATE JSONDoc SET Data = JSON_REPLACE(Data, CONCAT('$', %s), CAST(%s AS JSON)) WHERE DocID=%s"
stmt = "UPDATE JSONDoc SET Data = JSON_REPLACE(Data, CONCAT('$', %s), %s) WHERE DocID=%s"
cursor.execute(stmt, ("", b_data, docID))
cnx.commit()
cursor.close()
cnx.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.