用数据库api中mogrify方法处理sql中特殊转义字符
问题:以一个url(json解码后的,存在转义字符)为查询条件去数据表中查找内容 出现报错
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4193576978d6ce6268477540c043b1ba.png)
分析:\u0027为单引号“‘”的unicode转义表示 在sql语句中不能适用
介绍:mogrify可以将复杂的语句转换为正常的格式 用法见如下代码
import psycopg2
import psycopg2.extras
import json
import demjson
pg_conn = psycopg2.connect(database="abc", user="postgres", password="postgres", host="10.255.xxx.xxx", port="5432")
cur = pg_conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
param = "http://www.ebscn.com:7004//${%23w%3d%23context.get(\u0027com.opensymphony.xwork2.dispatcher.HttpServletResponse\u0027).getWriter(),%23w.print(\u0027Nessus%20Response:%20\u0027),%23w.println(\u0027struts_2_3_14_3_command_execution-1975557311\u0027),%23w.flush(),%23w.close()}.action"
check_sql = cur.mogrify("SELECT r_id FROM h_misreport_info WHERE record_info =%s",(param,))
cur.execute(check_sql)
res = cur.fetchone()
print(res)
pg_conn.close()