BIGPESH
BIGPESH

Reputation: 83

SQLite Query Multiple Column Search

I have a SQLite database which I would like to search two columns with one word (from a variable)

Currently I have the below code & query...

selection = locTextBox.get()
    cursor.execute("SELECT * FROM depot_racks WHERE rack OR floorzone LIKE '%' || ? || '%'", (selection,))
    for row in cursor.fetchall():
        dataArea.insert("", tk.END, values=row)

When the variable is empty it returns all results which is correct, when the variable is set to FZ1 it returns items found in the floor zone column but if the variable contains RACK1 then it returns nothing from the rack column.

The variable is set via a combobox which the user selects from (either RACK1, RACK2 etc or FZ1, FZ2 etc)

The query is obviously wrong in some way I just cannot figure it out, any help is appreciated!

Upvotes: 1

Views: 202

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10969

The OR is not associative like a "+" in arithmetic so you have to repeat the whole LIKE part.

cursor.execute("SELECT * FROM depot_racks WHERE rack LIKE '%' || ? || '%' OR floorzone LIKE '%' || ? || '%'", (selection, selection)) 

Upvotes: 2

Related Questions