Обсуждение: Why that query fails?

Поиск
Список
Период
Сортировка

Why that query fails?

От
Igor Korot
Дата:
Hi, ALL,
[quote]
Thread 1 "dbhandler" hit Breakpoint 1,
PostgresDatabase::CreateDatabase (this=0x616000180c80,
    name=L"test", opts=std::shared_ptr<CreateDBOptions> (use count 1,
weak count 0) = {...},
    errorMsg=std::vector of length 0, capacity 0) at
../../libpostgres/database_postgres.cpp:46
46        int result = 0;
(gdb) n
47        auto exists = false;
(gdb)
48        std::vector<std::wstring> dbList;
(gdb)
49        std::wstring qry0, qry1, qry2;
(gdb)
50        qry0 = L"SELECT 1 FROM pg_database WHERE datname = $1";
(gdb)
51        if( opts->m_exist )
(gdb)
54            values[0] = NULL;
(gdb)
55            values[0] = new char[name.length() * sizeof( wchar_t ) + 1];
(gdb)
56            memset( values[0], '\0', name.length() * sizeof( wchar_t ) + 1 );
(gdb)
57            strcpy( values[0], m_pimpl->m_myconv.to_bytes(
name.c_str() ).c_str() );
(gdb)
58            int len1 = (int) name.length() * sizeof( wchar_t );
(gdb)
59            int length[1] = { len1 };
(gdb)
60            int formats[1] = { 1 };
(gdb)
61            auto res = PQexecParams( m_db,
m_pimpl->m_myconv.to_bytes( qry0.c_str() ).c_str(), 1, NULL, values,
length, formats, 0 );
(gdb)
62            auto status = PQresultStatus( res );
(gdb)
63            if( status != PGRES_TUPLES_OK )
(gdb) p status
$1 = PGRES_FATAL_ERROR
(gdb) n
65                std::wstring err = m_pimpl->m_myconv.from_bytes(
PQerrorMessage( m_db ) );
(gdb)
66                errorMsg.push_back( L"Error executing query: " + err );
(gdb) p err
$2 = L"ERROR:  invalid byte sequence for encoding \"UTF8\":
0x00\nCONTEXT:  unnamed portal parameter $1\n"
(gdb)
[/quote]

What am I doing wrong?

Thank you.



Re: Why that query fails?

От
Adrian Klaver
Дата:
On 4/26/26 1:15 PM, Igor Korot wrote:
> Hi, ALL,
> [quote]
> Thread 1 "dbhandler" hit Breakpoint 1,

> 66                errorMsg.push_back( L"Error executing query: " + err );
> (gdb) p err
> $2 = L"ERROR:  invalid byte sequence for encoding \"UTF8\":

I would think the above is a clue.

Are you sure the value you are passing to the parameter is UTF8?

> 0x00\nCONTEXT:  unnamed portal parameter $1\n"
> (gdb)
> [/quote]
> 
> What am I doing wrong?
> 
> Thank you.
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com



Re: Why that query fails?

От
Igor Korot
Дата:
Adrian,

On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
>
> On 4/26/26 1:15 PM, Igor Korot wrote:
> > Hi, ALL,
> > [quote]
> > Thread 1 "dbhandler" hit Breakpoint 1,
>
> > 66                errorMsg.push_back( L"Error executing query: " + err );
> > (gdb) p err
> > $2 = L"ERROR:  invalid byte sequence for encoding \"UTF8\":
>
> I would think the above is a clue.
>
> Are you sure the value you are passing to the parameter is UTF8?

Positive.

This is what the following line should be doing:

[code]
57            strcpy( values[0], m_pimpl->m_myconv.to_bytes(
name.c_str() ).c_str() );
[/code]

Thank you.

>
> > 0x00\nCONTEXT:  unnamed portal parameter $1\n"
> > (gdb)
> > [/quote]
> >
> > What am I doing wrong?
> >
> > Thank you.
> >
> >
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com



Re: Why that query fails?

От
Igor Korot
Дата:
Adrian,

On Sun, Apr 26, 2026 at 3:34 PM Igor Korot <ikorot01@gmail.com> wrote:
>
> Adrian,
>
> On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> >
> > On 4/26/26 1:15 PM, Igor Korot wrote:
> > > Hi, ALL,
> > > [quote]
> > > Thread 1 "dbhandler" hit Breakpoint 1,
> >
> > > 66                errorMsg.push_back( L"Error executing query: " + err );
> > > (gdb) p err
> > > $2 = L"ERROR:  invalid byte sequence for encoding \"UTF8\":
> >
> > I would think the above is a clue.
> >
> > Are you sure the value you are passing to the parameter is UTF8?
>
> Positive.
>
> This is what the following line should be doing:
>
> [code]
> 57            strcpy( values[0], m_pimpl->m_myconv.to_bytes(
> name.c_str() ).c_str() );
> [/code]

And the conversion is as follows:

[code]
    std::wstring_convert<std::codecvt_utf8<wchar_t> > m_myconv;
[/code]

Thank you.

>
> Thank you.
>
> >
> > > 0x00\nCONTEXT:  unnamed portal parameter $1\n"
> > > (gdb)
> > > [/quote]
> > >
> > > What am I doing wrong?
> > >
> > > Thank you.
> > >
> > >
> >
> >
> > --
> > Adrian Klaver
> > adrian.klaver@aklaver.com



Re: Why that query fails?

От
Igor Korot
Дата:
Adrian,

On Sun, Apr 26, 2026 at 3:38 PM Igor Korot <ikorot01@gmail.com> wrote:
>
> Adrian,
>
> On Sun, Apr 26, 2026 at 3:34 PM Igor Korot <ikorot01@gmail.com> wrote:
> >
> > Adrian,
> >
> > On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> > >
> > > On 4/26/26 1:15 PM, Igor Korot wrote:
> > > > Hi, ALL,
> > > > [quote]
> > > > Thread 1 "dbhandler" hit Breakpoint 1,
> > >
> > > > 66                errorMsg.push_back( L"Error executing query: " + err );
> > > > (gdb) p err
> > > > $2 = L"ERROR:  invalid byte sequence for encoding \"UTF8\":
> > >
> > > I would think the above is a clue.
> > >
> > > Are you sure the value you are passing to the parameter is UTF8?
> >
> > Positive.
> >
> > This is what the following line should be doing:
> >
> > [code]
> > 57            strcpy( values[0], m_pimpl->m_myconv.to_bytes(
> > name.c_str() ).c_str() );
> > [/code]
>
> And the conversion is as follows:
>
> [code]
>     std::wstring_convert<std::codecvt_utf8<wchar_t> > m_myconv;
> [/code]
>
> Thank you.

Those are the values from gdb:

[code]
(gdb) p values[0]
$1 = 0x603001613380 "test"
(gdb) p strlen(values[0])
$2 = 4
(gdb)
[/code]

Thank you.

>
> >
> > Thank you.
> >
> > >
> > > > 0x00\nCONTEXT:  unnamed portal parameter $1\n"
> > > > (gdb)
> > > > [/quote]
> > > >
> > > > What am I doing wrong?
> > > >
> > > > Thank you.
> > > >
> > > >
> > >
> > >
> > > --
> > > Adrian Klaver
> > > adrian.klaver@aklaver.com



Re: Why that query fails?

От
Ron Johnson
Дата:
When in doubt, run the query interactively.

On Sun, Apr 26, 2026 at 6:06 PM Igor Korot <ikorot01@gmail.com> wrote:
Adrian,

On Sun, Apr 26, 2026 at 3:38 PM Igor Korot <ikorot01@gmail.com> wrote:
>
> Adrian,
>
> On Sun, Apr 26, 2026 at 3:34 PM Igor Korot <ikorot01@gmail.com> wrote:
> >
> > Adrian,
> >
> > On Sun, Apr 26, 2026 at 3:22 PM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> > >
> > > On 4/26/26 1:15 PM, Igor Korot wrote:
> > > > Hi, ALL,
> > > > [quote]
> > > > Thread 1 "dbhandler" hit Breakpoint 1,
> > >
> > > > 66                errorMsg.push_back( L"Error executing query: " + err );
> > > > (gdb) p err
> > > > $2 = L"ERROR:  invalid byte sequence for encoding \"UTF8\":
> > >
> > > I would think the above is a clue.
> > >
> > > Are you sure the value you are passing to the parameter is UTF8?
> >
> > Positive.
> >
> > This is what the following line should be doing:
> >
> > [code]
> > 57            strcpy( values[0], m_pimpl->m_myconv.to_bytes(
> > name.c_str() ).c_str() );
> > [/code]
>
> And the conversion is as follows:
>
> [code]
>     std::wstring_convert<std::codecvt_utf8<wchar_t> > m_myconv;
> [/code]
>
> Thank you.

Those are the values from gdb:

[code]
(gdb) p values[0]
$1 = 0x603001613380 "test"
(gdb) p strlen(values[0])
$2 = 4
(gdb)
[/code]

Thank you.

>
> >
> > Thank you.
> >
> > >
> > > > 0x00\nCONTEXT:  unnamed portal parameter $1\n"
> > > > (gdb)
> > > > [/quote]
> > > >
> > > > What am I doing wrong?
> > > >
> > > > Thank you.
> > > >
> > > >
> > >
> > >
> > > --
> > > Adrian Klaver
> > > adrian.klaver@aklaver.com




--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

Re: Why that query fails?

От
Greg Sabino Mullane
Дата:
You are using binary format, but your length calculation is wrong:

int len1 = (int) name.length() * sizeof( wchar_t );

Just use text format for a query like this.

Re: Why that query fails?

От
Tom Lane
Дата:
Greg Sabino Mullane <htamfids@gmail.com> writes:
> You are using binary format, but your length calculation is wrong:
> int len1 = (int) name.length() * sizeof( wchar_t );

I think the actual problem is that he's trying to send an array
of wchar_t to the server.  That is not bit-compatible with UTF-8,
even though they are both representations of Unicode.  The query
string itself has the same problem I think (I believe the L"..."
syntax in C produces an array of wchar_t).

            regards, tom lane