Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute LISTEN with parameters #432

Closed
momoneko opened this issue May 24, 2016 · 1 comment
Closed

Execute LISTEN with parameters #432

momoneko opened this issue May 24, 2016 · 1 comment
Labels

Comments

@momoneko
Copy link

When performing LISTEN on a channel, passing parameters results in a syntax error

>>> cursor.execute('Listen %s', ('hello',))`

ProgrammingError: syntax error at or near "'hello'"
LINE 1: Listen 'hello'

The only way I could get it to work is to % format it, which seems to be very frowned upon in the documentation, with good reason, and would prevent using executemany for multiple channels.

>>> cursor.execute('Listen %s'% ('hello',))

Is there any way to pass the channel as a parameter?

@dvarrazzo
Copy link
Member

Answered on the ML:

Currently no. On master there is a quote_ident feature that is what
you need to sanitize your query. If I manage to find some time to wrap
psycopg 2.7 it should be included in that release.

#359

Alternatively you may write a plpgsql function listen(channel text)
taking the argument as a string and calling "execute(format('LISTEN
%I', channel))" (untested) and call that function instead, using
normal psycopg arguments. The %I in the format() function should
escape bad strings as valid identifiers: see
http://www.postgresql.org/docs/9.3/static/functions-string.html#FUNCTIONS-STRING-FORMAT.
This would work with executemany too (the quote_ident() thing wouldn't
work for that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants