Re: Minor issue

Поиск
Список
Период
Сортировка
От Rory Campbell-Lange
Тема Re: Minor issue
Дата
Msg-id 20200526140936.GA28120@campbell-lange.net
обсуждение исходный текст
Ответ на Re: Minor issue  (Frank Millman <frank@chagford.com>)
Список psycopg
On 26/05/20, Frank Millman (frank@chagford.com) wrote:
> 
> 
> On 2020-05-26 3:08 PM, Rory Campbell-Lange wrote:
> > On 26/05/20, Frank Millman (frank@chagford.com) wrote:
> > Does using %s instead of {}.format help solve the issue?
> 
> To reproduce my situation, you should place the '--' at the beginning of the
> following line (',%s as d').
> 
> As no parameters are now being substituted, I would expect to supply an
> empty tuple. In fact, the parameter is still required.

That makes sense, apologies.

However, this works as you suggest:

    In [33]: d.query("""
        ...: select 
        ...:     1 as a
        ...:     /*
        ...:     ,2 as b
        ...:     */
        ...:     -- ,'hi' as c
        ...:     -- ,%s as d
        ...: """, ("a string", )).results
    Out[33]: [Record(a=1)]

This doesn't work (as you suggest):

    In [35]: d.query("""
        ...: select 
        ...:     1 as a
        ...:     /*
        ...:     ,2 as b
        ...:     */
        ...:     -- ,'hi' as c
        ...:     -- ,%s as d
        ...: """, ()).results
    ERROR:root:An unexpected error occurred while tokenizing input
    The following traceback may be corrupted or invalid
    The error message is: ('EOF in multi-line string', (1, 0))

But this works:

    In [34]: d.query("""
        ...: select 
        ...:     1 as a
        ...:     /*
        ...:     ,2 as b
        ...:     */
        ...:     -- ,'hi' as c
        ...:     -- ,%s as d
        ...: """).results
    Out[34]: [Record(a=1)]

So perhaps simply don't provide the tuple in this case? This will also probably
work for your other backends.

Rory



В списке psycopg по дате отправления:

Предыдущее
От: Daniele Varrazzo
Дата:
Сообщение: Async notifications in psycopg3
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: Async notifications in psycopg3