When using psycopg2 to query my database with Python, how can I correctly reference row values in an ILIKE statement?

Viewed 26

I have a database that I've successfully accessed using psycopg2, but I'm running into trouble correctly using ILIKE to compare a Python variable to row values from my database. Here's the code:

for i in range(1, len(fin_df)):
        cursor.execute("""WITH res as (SELECT year, symbol, model, make, progressive_symbol FROM \"Motorcycle\".motorcycles WHERE  %s ILIKE '%' || model || '%'), maxyr AS (SELECT MAX(year) FROM res) SELECT * FROM res WHERE year IN (SELECT * FROM maxyr)""", (fin_df.model[i],))

Which returns a tuple index out of range error. To clarify, the model variable is a row from the postgres table. If possible, I'd also like to figure out how to to do the reverse- as in, see where model ILIKE the Python variable. Thanks!

0 Answers
Related