c++cdatabasepostgresqlbytea

How can I get double(or bytes) data through PQgetvalue in an efficient way?


I create a table with 3 columns: int8, double and bytea. When query a row of data, I use PQgetvalue to get each column value in a row. But PQgetvalue return a char*, I have to convert text value to corresponding type, like atoi or strtod etc.

Is there is a more efficient way to get actual data, avoid converting data every time?


Solution

  • There is not much overhead in converting a char * to a number. But you can use binary mode by calling PQexecParams with 1 as the last argument.

    To retrieve binary data, you have to use PQgetlength and PQgetisnull to get size and NULLness of the datum. Also, the data will be in the binary format used by the database server, so the representation of a double could be different if both machines have a different hardware architecture (although IEEE is used pretty much everywhere).