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?

Z4-tier
  • 7,287
  • 3
  • 26
  • 42

1 Answers1

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()
Divya Prakash
  • 898
  • 1
  • 6
  • 14