Searching by keyword in sqlite3 and python

Question:

I am trying to search in my db by a keyword in sqlite3. But I am not able to do this. After creating the connection and cursor I use this line to try and get the columns I am looking for:
curs.execute(f"""SELECT * FROM {table_name} WHERE column1 LIKE {key_word}""").fetchall().
But when doing this I get the error:
sqlite3.OperationalError: no such column: test3.
Where test3 is the keyword I used here. I don’t understand this error since I want to search the column: "column1".
Can somebody please help me with this?

Asked By: elias

||

Answers:

Not clear what you are trying to do. In particular your usage of LIKE with a simple keyword.
But from a syntax point of view, you are missing some quotes (and probably some %, but that is up to you) around "test3".

curs.execute(f"""SELECT * FROM {table_name} WHERE column1 LIKE '{key_word}'""").fetchall()
Answered By: chrslg
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.