0

When I execute the below query in a query editor like DBeaver - it returns a result but if I execute the same query via Python & psycopg2 it does not return a result. '%%' should match any title/location so there will always return something. I'm just testing this for a category without keywords but it will also take an array of keywords if they exist depending on the category. So the array could be ['%%'] or ['%boston%', '%cambridge%'] and both should work.

select title, link
from internal.jobs 
where (title ilike any(array['%%'])
or location ilike any(array['%%']))
order by "publishDate" desc
limit 1;

I've tried adding the E flag at the beginning of the string. E.g. E'%%'

Python:

import psycopg2

FILTERS = {
    'AllJobs': [],
    'BostonJobs': ['boston', 'cambridge'],
    'MachineLearningJobs': ['ml', 'machine learning']
}

conn = psycopg2.connect("dbname=test user=postgres")
cur = conn.cursor()

sql = """
select title, link
from internal.jobs 
where (title ilike any(array[%s])
or location ilike any(array[%s]))
order by "publishDate" desc
limit 1;
"""

for title, tags in FILTERS.items():
    if not tags:
        formatted_filters = "'%%'" # Will match any record
    else:
        formatted_filters = ','.join([f"'%{keyword}%'" for keyword in tags])

    cur.execute(sql, (formatted_filters))
    results = cur.fetchone()
    print(results)
3
  • 1
    What is the Python code you're using? Commented Jan 30, 2020 at 1:52
  • 1
    You've got single quotes, double quotes, and percent signs so I highly suspect your issue is with a character that needs escaping but without seeing the Python code there's no way to know. Here's more info. Commented Jan 30, 2020 at 2:04
  • Thanks @AlexW I suspect you're correct and someone may point me in the right direction for escaping % signs / building an array this way. Commented Jan 30, 2020 at 3:05

2 Answers 2

1

You can use the cur.mogrify() query to look at the SQL finally generated, check in psql if it works, and how you need to tweak it.

Most likely you have to double every %.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to Piro for the very useful cur.mogrify() clue. That helped me further debug the query to figure out what was going wrong.

I ended up removing the extra set of quotes, I used a named parameter and now it works as expected.

Updated code:

import psycopg2

FILTERS = {
    'AllJobs': [],
    'BostonJobs': ['boston', 'cambridge'],
    'MachineLearningJobs': ['ml', 'machine learning']
}

conn = psycopg2.connect("dbname=test user=postgres")
cur = conn.cursor()

sql = """
select title, link
from internal.jobs 
where (title ilike any(array[%(filter)s])
or location ilike any(array[%(filter)s]))
order by "publishDate" desc
limit 1;
"""

for title, tags in FILTERS.items():
    if not tags:
        formatted_filters = '%%' # Will match any record
    else:
        formatted_filters = [f'%{keyword}%' for keyword in tags]

    print(cur.mogrify(sql, {'filter': formatted_filters}))
    cur.execute(sql, {'filter': formatted_filters})
    results = cur.fetchone()
    print(results)

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.