1

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!

1 Answer 1

2

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)) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.