Re: Parameter in SQL query being misinterpreted - Mailing list psycopg

From Adrian Klaver
Subject Re: Parameter in SQL query being misinterpreted
Date
Msg-id 567f0d1f-c130-4abb-b2e4-dd6abdf89549@aklaver.com
Whole thread Raw
In response to Re: Parameter in SQL query being misinterpreted  (Adrian Klaver <adrian.klaver@aklaver.com>)
List 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.klaver@aklaver.com




psycopg by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Parameter in SQL query being misinterpreted
Next
From: Daniele Varrazzo
Date:
Subject: Re: Parameter in SQL query being misinterpreted