【发布时间】:2015-06-22 15:55:02
【问题描述】:
我在 Django 1.8 中使用 Postgres 9.4 后端工作。我目前正在使用django.db.connection 运行以下查询:
cursor = connection.cursor()
codes = ['01', '02'] # these are actually obtained as GET parameters
query = "SELECT number_str, bnf_id, name FROM mytable WHERE "
for i, code in enumerate(codes):
q = "(number_str ILIKE '{}%' OR name ~* '{}') "
query += q.format(code, code)
if i < len(codes)-1:
query += 'OR '
cursor.execute(query)
这可行,但它是否容易受到 SQL 注入的影响?
如果是这样,有什么方法可以在将codes 传递到查询字符串之前转义它们,这仍然适用于这个正则表达式查询吗?
【问题讨论】:
标签: python regex django postgresql