Python 将json插入mysql。json字符串是从json.dumps获取的

Python 将json插入mysql。json字符串是从json.dumps获取的,python,mysql,json,Python,Mysql,Json,问题在于将json字符串插入MySQL数据库。 在我的python程序中,我通过json.dumpsd获得json,其中d是一个字典。插入代码是: query = ("INSERT INTO bm_triesJsons VALUES (%s, %s, %s);"% (article_id, revision_number, jsn)) print query cur.execute(query) 看起来问题是引号

问题在于将json字符串插入MySQL数据库。 在我的python程序中,我通过json.dumpsd获得json,其中d是一个字典。插入代码是:

        query = ("INSERT INTO bm_triesJsons VALUES (%s, %s, %s);"%
                 (article_id, revision_number, jsn))
        print query
        cur.execute(query)
看起来问题是引号,引号前面没有转义符号。
如何解决此问题?

在执行查询时使用参数化方法处理值。驾驶员将处理逃生:

query = "INSERT INTO bm_triesJsons VALUES (%s, %s, %s);"
cur.execute(query, (article_id, revision_number, jsn))
如果您希望直接和手动访问MySQLdb使用的转义函数,可以这样做:

c=MySQLdb.connection 打印c.escape_字符串“{foo:bar}” {\foo\:\bar\}
在执行查询时,使用参数化方法处理值。驾驶员将处理逃生:

query = "INSERT INTO bm_triesJsons VALUES (%s, %s, %s);"
cur.execute(query, (article_id, revision_number, jsn))
如果您希望直接和手动访问MySQLdb使用的转义函数,可以这样做:

c=MySQLdb.connection 打印c.escape_字符串“{foo:bar}” {\foo\:\bar\}