5

I am wondering if I can make parameterized queries directly from C/C++ with libpq instead of using strings and if do how should this code look's like?

string tblins = "";
tblins = "INSERT INTO " + commtable + " "
         "(vdoc, bdoc, mytime, txml) VALUES ("
         "'" + cxml.vdoc + "', "
             + cxml.bdoc + ", " //integer
         "'" + cxml.mytime + "', "
         "'" + cxml.txml + "')";

result = PQexec(conn, tblins.c_str());

Thanks.

1 Answer 1

4

Yes, you can use the PQexecParams function as explained in the documentation.

If parameters are used, they are referred to in the command string as $1, $2, etc. nParams is the number of parameters supplied; it is the length of the arrays paramTypes[], paramValues[], paramLengths[], and paramFormats[].

Sign up to request clarification or add additional context in comments.

1 Comment

it's also recommended you typecast parameters, so $1::integer, $2::varchar etc, so you can pass NULL for the paramTypes arg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.