title: How do I properly execute SQL queries with optional parameters in Python?
body: <p>I have an application where users are allowed to set filters on a set of data.  For instance:</p>
  
  <pre><code>cursor.execute("select * from products where price &lt;= %s", (price))
  </code></pre>
  
  <p>But I would like to have the possibility of optional filter elements.  For instance:</p>
  
  <pre><code>cursor.execute("select * from products where price &lt;= %s and size &gt;= %s and weight &lt;= %s", (price,size,weight))
  </code></pre>
  
  <p>What is the proper way of constructing this query?  It is possible to use the % wildcard as a value, but this seems very hacky.  Users may specify one, several, or none of the filters.</p>