Python 裡 mysql 的 backsplash 跳脫

Posted in :

直接下sql 指令給 mysql:

insert into t(c) VALUES (‘{“City”: “Galle”, “Description”: “Best damn city in the world”}’)

存入 database 裡的值合乎預期。但透過 python + mysql.connector 執行:

 cmd4 = '{"City": "Galle", "Description": "Best damn city in the world"}'
 cursor=db.cursor()
 sql = "insert into t(c) Values(%s);"
 cursor.execute(sql, (cmd4,))

結果出乎預期,backsplash 被跳脫!

看了一下官方的source code:

哦買尬,完全是 hard code 沒有設定可以改。backsplash 不跳脫…

較快速的解法有 2:

  • 1:修改第3方package source code。
  • 2:允許 mysql backsplash 可以跳脫。

 

相關文章:

Mysql 的 NO_BACKSLASH_ESCAPES
http://stackoverflow.max-everyday.com/2017/08/mysql-no_backslash_escapes/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *