0

In my flask app I am trying to pass a string to the query below:

tag = "Abcd"

conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM database WHERE message LIKE %s ESCAPE ''", (tag,))
data = cur.fetchall()
cur.close()
conn.close()

I receive blank result.

I use psycopg2. My Postgres database has message containing "Abcd" and it works fine if I simply do:

cur.execute("SELECT * FROM database WHERE message LIKE '%Abcd%'")

If I try to pass it as a variable "tag" is my syntax for LIKE query correct?

2
  • You must add the percent signs then: tag = "%Abcd%". Commented Feb 19, 2023 at 1:17
  • LIKE '%%'||%s||'%%'. Commented Feb 19, 2023 at 16:51

1 Answer 1

0

you need to add % in your tag.

Try this:

tag = '%Abcd%' #--->> change here, add %Abcd%

conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM database WHERE message LIKE %s ESCAPE ''", (tag,))
data = cur.fetchall()
cur.close()
conn.close()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.