mysql插入json数据

data_dict = {"a":1, "b":2} 

data_json = json.dumps(data_dict)

data_escape = MySQLdb.escape_string(data_json)   # 需要进行编码,否则无法插入

sql = "insert into <table_name> (<cloumn_name, >) values ('%s')" % data_escape 

关键步骤在与在json.dumps()之后,进行编码:data_escape = MySQLdb.escape_string(data_json),将json字符串中的引号等字符进行转义。 

原文地址:https://www.cnblogs.com/buxizhizhoum/p/8024684.html