在python中使用pymysql踩过的坑

在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)


    

发布了63 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_30966497/article/details/103720439