sqlite and python filter results
Question: I have a python function to do query from sqlite. as the following:

    
    
    def Query(X, Y, Z):
    

where X, Y & Z are the columns of the database. so for example when

    
    
    Query(1, 2, 3)
    

it will go to the sqlite as follows:

    
    
    <code>SELECT OUTPUT FROM IMAGES WHERE (X=?) AND (Y=?) AND (Z=?)",[X,Y,Y]</code>
    

So, it will take numbers 1,2 & 3 and will fetch them from sqlite. The problem
I have when the input of:

    
    
    Query(X, Y, Z)
    

has one or more empty fields, as example:

    
    
    Query(1, 2, '')
    

in this case I want the database to ignore column Z, but I can not figure out
how to do it. I have tried

    
    
    SELECT OUTPUT FROM IMAGES WHERE (X=? OR X) AND (Y=? OR Y) AND (Z=? OR Z)",[X,Y,Z]
    

but this statement is not working well.

Could someone help, how to ignore a column if the input from Python is empty.


Answer: There could be something wrong with the testing of an empty string. You could
try something similar to the following:

    
    
    "SELECT OUTPUT FROM IMAGES WHERE (X=? OR ?='') AND (Y=? OR ?='') AND (Z=? OR ?='')",[X,Y,Z]
    

But admittedly it is more like a hint than an actual verified solution.

**Edit**

Probably you need named parameters:

    
    
    def Query(X, Y, Z):
        import sqlite3
        conn = sqlite3.connect('example.db')
        c = conn.cursor()
        c.execute("SELECT OUTPUT FROM IMAGES WHERE (X=:px OR :px='') AND (Y=:py OR :py='') AND (Z=:pz OR :pz='')", {"px": X, "py": Y, "pz": Z})
    

**Edit**

Try this [sqlfiddle](http://sqlfiddle.com/#!7/1a462/1).

**Edit**

Consider the following fragment of your query:

    
    
    (Y=? OR Y)
    

For a database row for which Y='foo', it is equivalent to:

    
    
    ('foo'=? OR 'foo')
    

Since 'foo' converts to false, that is equivalent to:

    
    
    ('foo'=? OR false)
    

Which is equivalent to:

    
    
    ('foo'=?)
    

Which can be true only if you queried for Y='foo'. If instead you queried
Y='', you get:

    
    
    ('foo'='')
    

Which is obviously false.

Hence your query is really behaving like the original one, the one without
ORs.

