| From: | Igor Korot <ikorot01(at)gmail(dot)com> |
|---|---|
| To: | pgsql-general(at)lists(dot)postgresql(dot)org |
| Subject: | Re: PQexecPrepared() question |
| Date: | 2025-12-22 01:30:38 |
| Message-ID: | CA+FnnTxVZ64Lrq3t35j7w8mE=oP94TgetT62+Y8=ea_VfE2uEw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
Hi,
On Sun, Dec 21, 2025 at 1:19 PM Peter J. Holzer <hjp-pgsql(at)hjp(dot)at> wrote:
>
> On 2025-12-21 00:39:23 -0500, Tom Lane wrote:
> > Igor Korot <ikorot01(at)gmail(dot)com> writes:
> > > I added the following code in my app:
> > > res = PQexec( m_db, "SHOW client_encoding" );
> > > auto value = PQgetvalue( res, 0, 0 );
> > > PQclear( res );
> > > and the value of the "value" variable is "UTF8".
> >
> > Okay ...
> >
> > > The exact error message is:
> > > ERROR: invalid byte sequence for encoding UTF8: 0xdf
> > > CONTEXT: unnamed portal parameter $1
> > > on the INSERT.
> >
> > client_encoding governs both the encoding that the server will
> > send, and the encoding that it expects to receive. You are
> > sending a parameter string that is not valid UTF8.
>
> As I see it, Igor has two options:
>
> 1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
> current locale which is obviously not using UTF-8. I don't program in
> C++, but I'm pretty sure there is a portable way to encode a string
> in UTF-8.
> 2) Figure out which encoding his locale is using and then set
> client_encoding appropriately. The first step to do that is to find
> the current locale.
> Igor, add
> #include <ctype.h>
> ...
> char *current_locale = setlocale(LC_CTYPE, "");
> to your code. What's the value of current_locale?
>
> I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
> for over 30 years ;-).
Maybe all I need is something like this:
convert(char *dest, const std::wstring src)
{
const wchar_t *temp = src.c_str();
while( *dest )
{
dest++;
}
while( *temp )
{
*dest = *temp;
dest++;
temp++;
}
*dest++ = '\0';
*dest = '\0';
}
?
Thank you.
>
> hjp
>
> --
> _ | Peter J. Holzer | Story must make more sense than reality.
> |_|_) | |
> | | | hjp(at)hjp(dot)at | -- Charles Stross, "Creative writing
> __/ | http://www.hjp.at/ | challenge!"
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Laurenz Albe | 2025-12-22 08:18:57 | Re: PQexecPrepared() question |
| Previous Message | Peter J. Holzer | 2025-12-21 21:19:25 | Re: PQexecPrepared() question |