Re: Minor issue

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Frank Millman <frank(at)chagford(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: Minor issue
Date: 2020-05-26 14:09:36
Message-ID: 20200526140936.GA28120@campbell-lange.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 26/05/20, Frank Millman (frank(at)chagford(dot)com) wrote:
>
>
> On 2020-05-26 3:08 PM, Rory Campbell-Lange wrote:
> > On 26/05/20, Frank Millman (frank(at)chagford(dot)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

In response to

Browse psycopg by date

  From Date Subject
Next Message Adrian Klaver 2020-05-28 16:47:44 Re: Async notifications in psycopg3
Previous Message Daniele Varrazzo 2020-05-26 13:22:15 Async notifications in psycopg3