Re: PQexecParams and "SET TIME ZONE $1" gets 'syntax error at or near "$1" at character 15'

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: ma(dot)sao(at)msa(dot)hinet(dot)net
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: PQexecParams and "SET TIME ZONE $1" gets 'syntax error at or near "$1" at character 15'
Date: 2024-11-23 15:57:49
Message-ID: CAFj8pRD2cBkTKafBYn1vUDSYp8CN_3XVk4+NA55g4J5jCHrC_Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

so 23. 11. 2024 v 16:01 odesílatel <ma(dot)sao(at)msa(dot)hinet(dot)net> napsal:

> I get get this same error
>
> syntax error at or near "$1" at character 15
>
> if I feed "const char *command" with the following texts.
>
> SET TIME ZONE $1
> SET TIME ZONE $1::TEXT
>
> For some reasons, I can not add quotes around $1 as follows.
>
> SET TIME ZONE '$1'
> SET TIME ZONE '$1'::TEXT
>
> Statements like "SELECT ... WHERE $1=..." have been working flawlessly.
>
> It looks like PQexecParams doesn't handle statement with "SET ... $1".
>
> Are there workarounds for me or PQexecParams needs improvement?
>
This statement has no plan, and then doesn't support parametrization.

you can use a function set_config instead
https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-SET

SELECT set_config('timezone', $1, false)

Regards

Pavel

> Thank you!
>
> --- 本郵件來自HiNet WebMail ---
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2024-11-23 16:19:04 Re: Database stats ( pg_stat_database.stats_reset ) get reset on daily basis - why?
Previous Message ma.sao 2024-11-23 15:01:04 PQexecParams and "SET TIME ZONE $1" gets 'syntax error at or near "$1" at character 15'