1

I am using python 2.7 and postgresql 9.3 with psycopg2 2.7.3, when I try to execute a select query while parameter passing it gives me this error psycopg2.ProgrammingError: syntax error at or near "OR

cur = con.cursor()
cur.execute('SELECT * FROM test WHERE voucher= ? OR voucher= ?', ('RHAT', 'MSO'))

the error message is

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

1 Answer 1

5

Psycopg2 uses %s placeholders, not ? questionmarks:

cur.execute(
    'SELECT * FROM test WHERE voucher = %s OR voucher = %s',
    ('RHAT', 'MSO'))

See the Passing parameters to SQL queries section of the Psycopg2 documentation.

Python database adapters generally stick with one or the other style (with accompanying named parameter styles). Always check the documentation to see what style is used. There is a paramstyle variable on the library, but that doesn't always reflect support for multiple styles very well.

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

3 Comments

When I trying to pass table name into a query it dosn't seem to work cur.execute('DROP TABLE %s ;', ('test',))
@Sameesh: a table name is not data, no. A placeholder escapes data specifically to not be mistaken for SQL instructions or object names.
See Pass column name as parameter to PostgreSQL using psycopg2 for a method to include a tablename.

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.