public inbox for [email protected]  
help / color / mirror / Atom feed
Re: Error on query execution
5+ messages / 2 participants
[nested] [flat]

* Error on query execution
@ 2025-03-05 07:42 Igor Korot <[email protected]>
  2025-03-05 02:37 ` Re: Error on query execution Tom Lane <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: Igor Korot @ 2025-03-05 07:42 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <[email protected]>

Hi, ALL,
[code]
    type = 80;
    uint32_t binaryIntVal;
    const char *paramValues[1];
    int paramLengths[1];
    int paramFormats[1];
    binaryIntVal = htonl( (uint32_t) type );
    paramValues[0] = (char *) &binaryIntVal;
    paramLengths[0] = sizeof( binaryIntVal );
    paramFormats[0] = 1;
    res = PQexecParams( m_db, "SELECT * FROM abcatfmt WHERE abf_type =
$1", 1, nullptr, paramValues, paramLengths, paramFormats, 1 );
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        std::wstring err = m_pimpl->m_myconv.from_bytes(
PQerrorMessage( m_db ) );
        errorMsg.push_back( L"Error executing query: " + err );
        result = 1;
    }
[/code]

Running the above I'm getting:

[quote]
(gdb) p err
$1 = L"ERROR:  incorrect binary data format in bind parameter 1\n"
[/quote]

What is wrong and how do I fix it?

Thank you.






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error on query execution
  2025-03-05 07:42 Error on query execution Igor Korot <[email protected]>
@ 2025-03-05 02:37 ` Tom Lane <[email protected]>
  2025-03-06 00:55   ` Re: Error on query execution Igor Korot <[email protected]>
  2025-03-06 21:54   ` Re: Error on query execution Igor Korot <[email protected]>
  0 siblings, 2 replies; 5+ messages in thread

From: Tom Lane @ 2025-03-05 02:37 UTC (permalink / raw)
  To: Igor Korot <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]>

Igor Korot <[email protected]> writes:
>     binaryIntVal = htonl( (uint32_t) type );
>     paramValues[0] = (char *) &binaryIntVal;
>     paramLengths[0] = sizeof( binaryIntVal );
>     paramFormats[0] = 1;

You're apparently trying to pass this parameter as an int4 ...

>     res = PQexecParams( m_db, "SELECT * FROM abcatfmt WHERE abf_type =
> $1", 1, nullptr, paramValues, paramLengths, paramFormats, 1 );

... but given that you didn't specify any data type, I think the
parser will fall back to assuming that $1 is the same type as
"abf_type", whatever that is.  Passing data in binary is not at all
forgiving about getting the data type right.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error on query execution
  2025-03-05 07:42 Error on query execution Igor Korot <[email protected]>
  2025-03-05 02:37 ` Re: Error on query execution Tom Lane <[email protected]>
@ 2025-03-06 00:55   ` Igor Korot <[email protected]>
  1 sibling, 0 replies; 5+ messages in thread

From: Igor Korot @ 2025-03-06 00:55 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]>

Tom,

On Tue, Mar 4, 2025, 8:37 PM Tom Lane <[email protected]> wrote:

> Igor Korot <[email protected]> writes:
> >     binaryIntVal = htonl( (uint32_t) type );
> >     paramValues[0] = (char *) &binaryIntVal;
> >     paramLengths[0] = sizeof( binaryIntVal );
> >     paramFormats[0] = 1;
>
> You're apparently trying to pass this parameter as an int4 ...
>

I thought only binary or text is allowed as parameters.

Guess I was wrong...

Thank you.


> >     res = PQexecParams( m_db, "SELECT * FROM abcatfmt WHERE abf_type =
> > $1", 1, nullptr, paramValues, paramLengths, paramFormats, 1 );
>
> ... but given that you didn't specify any data type, I think the
> parser will fall back to assuming that $1 is the same type as
> "abf_type", whatever that is.  Passing data in binary is not at all
> forgiving about getting the data type right.
>
>                         regards, tom lane
>


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error on query execution
  2025-03-05 07:42 Error on query execution Igor Korot <[email protected]>
  2025-03-05 02:37 ` Re: Error on query execution Tom Lane <[email protected]>
@ 2025-03-06 21:54   ` Igor Korot <[email protected]>
  2025-03-06 17:00     ` Re: Error on query execution Tom Lane <[email protected]>
  1 sibling, 1 reply; 5+ messages in thread

From: Igor Korot @ 2025-03-06 21:54 UTC (permalink / raw)
  To: Tom Lane <[email protected]>; pgsql-generallists.postgresql.org <[email protected]>

H,

On Tue, Mar 4, 2025 at 8:37 PM Tom Lane <[email protected]> wrote:
>
> Igor Korot <[email protected]> writes:
> >     binaryIntVal = htonl( (uint32_t) type );
> >     paramValues[0] = (char *) &binaryIntVal;
> >     paramLengths[0] = sizeof( binaryIntVal );
> >     paramFormats[0] = 1;
>
> You're apparently trying to pass this parameter as an int4 ...
>
> >     res = PQexecParams( m_db, "SELECT * FROM abcatfmt WHERE abf_type =
> > $1", 1, nullptr, paramValues, paramLengths, paramFormats, 1 );
>
> ... but given that you didn't specify any data type, I think the
> parser will fall back to assuming that $1 is the same type as
> "abf_type", whatever that is.  Passing data in binary is not at all
> forgiving about getting the data type right.

Changing the line above to read:

    res = PQexecParams( m_db, "SELECT * FROM abcatfmt WHERE abf_type =
$1::smallint", 1, nullptr, paramValues, paramLengths, paramFormats, 1
);


results in:

$1 = L"ERROR:  incorrect binary data format in bind parameter 1\n"

I am now at a complete loss.

How do I fix the code so it will run?

Thank you.

>
>                         regards, tom lane






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Error on query execution
  2025-03-05 07:42 Error on query execution Igor Korot <[email protected]>
  2025-03-05 02:37 ` Re: Error on query execution Tom Lane <[email protected]>
  2025-03-06 21:54   ` Re: Error on query execution Igor Korot <[email protected]>
@ 2025-03-06 17:00     ` Tom Lane <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2025-03-06 17:00 UTC (permalink / raw)
  To: Igor Korot <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]>

Igor Korot <[email protected]> writes:
> On Tue, Mar 4, 2025 at 8:37 PM Tom Lane <[email protected]> wrote:
>> ... but given that you didn't specify any data type, I think the
>> parser will fall back to assuming that $1 is the same type as
>> "abf_type", whatever that is.  Passing data in binary is not at all
>> forgiving about getting the data type right.

> Changing the line above to read:

>     res = PQexecParams( m_db, "SELECT * FROM abcatfmt WHERE abf_type =
> $1::smallint", 1, nullptr, paramValues, paramLengths, paramFormats, 1
> );

That just makes it explicit that the query is expecting an int16.
You're still passing an int32.  You need to either change the
parameter setup code to pass an int16, or make the query look
more like

    res = PQexecParams( m_db, "SELECT * FROM abcatfmt WHERE abf_type =
    $1::integer", 1, nullptr, paramValues, paramLengths, paramFormats, 1);

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2025-03-06 21:54 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-05 07:42 Error on query execution Igor Korot <[email protected]>
2025-03-05 02:37 ` Tom Lane <[email protected]>
2025-03-06 00:55   ` Igor Korot <[email protected]>
2025-03-06 21:54   ` Igor Korot <[email protected]>
2025-03-06 17:00     ` Tom Lane <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox