Re: Parameter in SQL query being misinterpreted

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Daniel Johnson <djohnson(at)progman(dot)us>, psycopg(at)lists(dot)postgresql(dot)org
Subject: Re: Parameter in SQL query being misinterpreted
Date: 2024-12-06 02:15:43
Message-ID: 567f0d1f-c130-4abb-b2e4-dd6abdf89549@aklaver.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: psycopg

On 12/5/24 18:00, Adrian Klaver wrote:
> On 12/5/24 17:37, Adrian Klaver wrote:
>> On 12/5/24 17:27, Daniel Johnson wrote:

>> You might try something like:
>>
>> NOW() + INTERVAL || ' ' ||  %(NextDBLog)s || ' SECOND'
>>
>> If that does work then you will need to use the psycopg.sql module to
>> build that part of the query
>>>
>
>
> What I ended getting to work:
>
> from psycopg import sql
>
> cur.execute(sql.SQL("select now(), now() + ({} || ' seconds')::interval
> ").format(sql.Literal(4)))
>
> cur.fetchone()
> (datetime.datetime(2024, 12, 5, 17, 57, 55, 670218,
> tzinfo=zoneinfo.ZoneInfo(key='US/Pacific')),
>  datetime.datetime(2024, 12, 5, 17, 57, 59, 670218,
> tzinfo=zoneinfo.ZoneInfo(key='US/Pacific')))
>
>

Then there is the alternative I keep forgetting about, the function
make_interval():

https://www.postgresql.org/docs/current/functions-datetime.html

make_interval ( [ years int [, months int [, weeks int [, days int [,
hours int [, mins int [, secs double precision ]]]]]]] ) → interval

Then you could pass parameters to the function.

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2024-12-06 02:39:31 Re: Parameter in SQL query being misinterpreted
Previous Message Adrian Klaver 2024-12-06 02:00:00 Re: Parameter in SQL query being misinterpreted