Re: Minor issue - Mailing list psycopg

From Rory Campbell-Lange
Subject Re: Minor issue
Date
Msg-id 20200526140936.GA28120@campbell-lange.net
Whole thread Raw
In response to Re: Minor issue  (Frank Millman <frank@chagford.com>)
List 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 by date:

Previous
From: Daniele Varrazzo
Date:
Subject: Async notifications in psycopg3
Next
From: Adrian Klaver
Date:
Subject: Re: Async notifications in psycopg3