Sameesh
Sameesh

Reputation: 353

psycopg2.ProgrammingError: syntax error at or near "OR

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"

Upvotes: 1

Views: 4266

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124338

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.

Upvotes: 5

Related Questions