Every time you don't know if a parameter will be NULL
you can use the following pattern:
SELECT * FROM table WHERE col = <parameter> OR (col IS NULL AND <parameter> IS NULL)
where <parameter>
is your parameter.
But note that I am using the same parameter two times in a row, so you need to switch to the the dict
format. The full code is:
sql = 'SELECT * FROM table WHERE col = %(p)s OR (col IS NULL AND %(p)s IS NULL)'
possible_params = [{"p":1}, {"p":None}]
for x in possible_params:
print cur.mogrify(sql,(x,))