使用原生SQL,当有多个POST参数传入时,怎么设置%S

使用原生SQL,然后很多条件使用传入的POST参数,如果按顺序填写,很难确保正确: 按官方的教程是这样写 cursor.execute("select ........ where a = %s and b = %s and c =%s", [pa, pa, pc]) 但是当我的条件比较多的时候,我很难确定自己的顺序写对没有,特别是有重复的,例如上面的a和b都使用的是pa的值 我查到的资料是python的,可以使用%(pa)s和字典来解决,但在django中无法使用 cursor.execute("select ........ where a = %(pa)s and b = %(pa)s and c =%(pc)s", {'pa':pa, 'pc':pc}) 错误提示是:format requires a mapping

评论 1

  • 今天早上起来突然想起来,虽然我是在django官网找的教程,但views里用的是python的语法,python里的格式化,现在用的是format

    修改为cursor.execute("select ........ where a = {pa} and b = {pa} and c = {pc}".format(pa='pa',pc='pc'))

    ok了

    还的注意把 , 改为 .