在python中使用pymysql踩過的坑
當你的sql中使用到%()s來接收dict格式的參數時,無論是有沒有使用到的key,key的值都不能是字典列表[{}],
否則會報TypeError: sequence item 0: expected str instance, dict found錯誤,其它的二元數組[[]]、字典{{}}等都不會報錯。
舉例:
# 只要param中包含像c這樣的字典列表[{}]值,無論sql中有沒有使用這個參數都會報錯
param = {"a": 11, "b": ['x','z'], "c": [{"d": 1}], "d": {"f": {"e": 1}}}
sql = """
select * from table where true
and a = %(a)s
and b = %(b)s
-- and c = %(c)s
and d = %(d)s
"""
cur.execute(sql, param)