0

My SQL query looks like this:

product = 'Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count)'
retailer = 'Target'
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)

conn = psycopg2.connect("dbname='test1' user='postgres' host='localhost' password='123'")
cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
cur.execute(query)

When i execute that i get a error saying:

psycopg2.ProgrammingError: syntax error at or near "Basic"

I am not sure why my syntax is wrong

1 Answer 1

2

Your statement;

query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)

...builds a complete string from the query and parameters without any quoting around your strings, which makes the entire string invalid SQL which fails at execute;

SELECT * FROM product_info 
WHERE product_name = Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count) 
  AND retailer = Target

What you're probably trying to do is parameterizing your query which is instead done in execute by passing the parameters in a tuple;

query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s"""
...
cur.execute(query, (product, retailer))
Sign up to request clarification or add additional context in comments.

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.