Syntax error in python sqlite3

I get syntax error when making the following sqlite3 query in python. Alias, Title and matID are the columns of table Total. like_var is the filter variable.

cursor.execute("""
‘SELECT rowid, Alias, Title, matID FROM Total WHERE Alias LIKE (?)’, (’%’ + like_var + ‘%’,)
UNION
‘SELECT rowid, Alias, Title, matID FROM Total WHERE Title LIKE (?)’, (’%’ + like_var + ‘%’,)
UNION
‘SELECT rowid, Alias, Title, matID FROM Total WHERE matID LIKE (?)’, (’%’ + like_var + ‘%’,)
“”")

  1. Why are we unioning a bunch of stuff together instead of just one big OR?
  2. Why’s there a comma at the end of your parameter inside the parenthesis?
  3. Why are we wrapping LIKE strings in parenthesis?

Wouldn’t it just be…

cursor.execute("SELECT rowid, Alias, Title, matID FROM Total WHERE Alias LIKE ? OR Title LIKE ? OR matID LIKE ?",("%"+like_var+"%","%"+like_var+"%","%"+like_var+"%"))
1 Like

I am learning, I am trying.
It works fine. Thank you very much.

1 Like

Wherever you learned to do it that way isn’t a reliable source of education for relational databases.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.