在 Python psycopg2 sql 模块中自动设置 = 和 IS
Automatically set = and IS in Python psycopg2 sql module
我使用 psycopg2 在 Python 中有这个片段:
spam = None
command = sql.SQL("= {}").format(sql.Literal(spam))
修改命令给出“= NULL”,当包含在 SQL 查询中时不会给出任何结果,因为查询需要 "IS NULL" 才能工作。
(如果垃圾邮件不是 None,但例如 "eggs",“= 'eggs'” 将适用于查询。)
有没有办法在mogrified命令中自动切换"IS"和“=”,取决于后面的参数是None还是None?
谢谢!
否,但您可以使用 is distinct from
而不是 =
来在比较中包含空值。
https://www.postgresql.org/docs/current/static/functions-comparison.html
我使用 psycopg2 在 Python 中有这个片段:
spam = None
command = sql.SQL("= {}").format(sql.Literal(spam))
修改命令给出“= NULL”,当包含在 SQL 查询中时不会给出任何结果,因为查询需要 "IS NULL" 才能工作。
(如果垃圾邮件不是 None,但例如 "eggs",“= 'eggs'” 将适用于查询。)
有没有办法在mogrified命令中自动切换"IS"和“=”,取决于后面的参数是None还是None?
谢谢!
否,但您可以使用 is distinct from
而不是 =
来在比较中包含空值。
https://www.postgresql.org/docs/current/static/functions-comparison.html