1、采用sql拼接的方式,拼接时用 xx= %s的方式,入参格式为元组或者list
如下举例:
sql = "select count(*) as sys_num from test where true "
params = []
if param1:
sql += " and param1 = %s "
params.append(param1)
if param2:
sql += " and (param2=%s) "
params.append(param2)
cur.execute(sql, params)
2、采用sql拼接的方式,拼接时用 xx=%(xx)s的方式, 入参格式为dict
如下举例:
sql = "select count(*) as sys_num from test where true "
params = {"param1": "xx", "param2": "xxx"}
if param1:
sql += " and param1 = %(param1)s "
if param2:
sql += " and (param2=%(param2)s) "
cur.execute(sql, params)
python中pymysql执行sql的两种可防止sql注入漏洞的传参方式(1、元组/列表 2、dict字典)
最新推荐文章于 2024-03-06 11:17:14 发布