Python psycopg2字符串插值格式vs%
根据psycopg2文档()的规定:Python psycopg2字符串插值格式vs%,python,psycopg2,Python,Psycopg2,根据psycopg2文档()的规定: Warning Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint. 在警告中,它特别提到不执行以下操作: cur.execute(SQL % data) 此警告是否也适用于以下使用格式的情况 c
Warning Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint.
在警告中,它特别提到不执行以下操作:
cur.execute(SQL % data)
此警告是否也适用于以下使用格式的情况
cur.execute(SQL.format(data))
我不知道format的内部结构,但我假设它下面使用了%string interop,这将使它的使用变得不明智SQL.format()
(其中SQL
是一个常规的ol'Python字符串)实际上没有在引擎盖下使用%
插值,但它也有同样的陷阱:您在中替换的值没有正确地转义为SQL(怎么可能;Python不知道SQL
是SQL语句),并且您的SQL语句可能会受到注入攻击
您的各种SQL模块都有防止此问题的方法,您应该改用它们。是的,确实如此。字符串插值方法很脆弱,因为它们允许SQL注入攻击。在参数化查询中使用外部(用户提供的)数据作为参数(而不是构建查询字符串),使得这种攻击不可能发生。我相信您的意思是可能的*而不是impossible@user1077071澄清。