Thread: Prepared statements aren't working with parameters with PQexecParams

Prepared statements aren't working with parameters with PQexecParams

From
"Subspace god"
Date:
The following works executed in the query browser:

PREPARE myquery (text) AS INSERT INTO myTable (word) VALUES ($1);
EXECUTE myquery('blah');

The following works in C++, executed as two statements with PQexecParams using PQEXECPARAM_FORMAT_BINARY

PREPARE myquery AS INSERT INTO myTable (word) VALUES ('blah');
EXECUTE myquery;

The following does not work in C++, executed as above, in another session using two subsequent calls to PQexecParams

PREPARE myquery (text) AS INSERT INTO myTable (word) VALUES ($1);
EXECUTE myquery($1::text);

The error is:

+        lastError    0x00dc4232 "ERROR:  bind message supplies 1 parameters, but prepared statement "" requires 0
"    char [1024]

Code is:

PQexecParams(pgConn, query.C_String(),indices.Size(),0,paramData,paramLength,paramFormat,PQEXECPARAM_FORMAT_BINARY);

Parameters:

+        paramData[0]    0x00e00208 "blah"    char *
        paramLength[0]    4    int
        paramFormat[0]    1    int
        indices.Size()    1    unsigned int

The difference between the two C++ calls is that the 2nd call has parameters passed to the prepared statement, while the first does not.

Any ideas? Note that it is saying that the prepared statement is "", rather than the name of the prepared statement. It also says 0 parameters, although it takes 1 parameter.

Re: Prepared statements aren't working with parameters with PQexecParams

From
"David Wilson"
Date:
On Fri, Sep 5, 2008 at 2:52 AM, Subspace god <subspacegod@gmail.com> wrote:

> The following does not work in C++, executed as above, in another session
> using two subsequent calls to PQexecParams
>
> PREPARE myquery (text) AS INSERT INTO myTable (word) VALUES ($1);
> EXECUTE myquery($1::text);

You're doing prepared statements incorrectly. Use PQprepare() and
PQexecPrepared() when using them from libpq; don't do them by hand on
your own.

--
- David T. Wilson
david.t.wilson@gmail.com