Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the double value using libpq?
    primarykey
    data
    text
    <p>The <a href="http://www.postgresql.org/docs/9.3/interactive/libpq-example.html" rel="nofollow">examples</a> in the libpq documentation show how to get the the integer value by converting it to the host-endian representation.</p> <p>I am curious what must be done to get the double precision value using libpq (without libpqtyppes)? I have tried <code>reinterpret_cast</code> with no luck.</p> <p>Also why text and byte data doesn't need endian conversions?</p> <p>The DB runs locally on Windows 7, I am using Visual C++ 2013.</p> <p><code>pptr</code> is a double vaule I am trying to retrieve.</p> <pre><code>#include &lt;iostream&gt; #include &lt;memory&gt; #include &lt;vector&gt; #include &lt;libpq-fe.h&gt; #include &lt;Winsock2.h&gt; static void show_binary_results(PGresult *res) { int i, j; int i_fnum, n_fnum, p_fnum; /* Use PQfnumber to avoid assumptions about field order in result */ i_fnum = PQfnumber(res, "id"); n_fnum = PQfnumber(res, "name"); p_fnum = PQfnumber(res, "price"); for (i = 0; i &lt; PQntuples(res); i++) { char* iptr; char* nptr; char* pptr; int blen; int ival; /* Get the field values (we ignore possibility they are null!) */ iptr = PQgetvalue(res, i, i_fnum); nptr = PQgetvalue(res, i, n_fnum); pptr = PQgetvalue(res, i, p_fnum); /*THIS IS A VALUE I AM TRYING TO GET*/ /* * The binary representation of INT4 is in network byte order, which * we'd better coerce to the local byte order. */ ival = ntohl(*((uint32_t *) iptr)); /* * The binary representation of TEXT is, well, text, and since libpq * was nice enough to append a zero byte to it, it'll work just fine * as a C string. * * The binary representation of BYTEA is a bunch of bytes, which could * include embedded nulls so we have to pay attention to field length. */ //blen = PQgetlength(res, i, b_fnum); printf("tuple %d: got\n", i); printf(" i = (%d bytes) %d\n", PQgetlength(res, i, i_fnum), ival); printf(" t = (%d bytes) '%s'\n", PQgetlength(res, i, n_fnum), nptr); printf(" p = (%d bytes) %f\n", PQgetlength(res, i, p_fnum), *reinterpret_cast&lt;double*&gt;(pptr)); printf("\n\n"); } } int main(int argc, char* argv []) { auto conn_string = "postgresql://postgres:pwd@localhost/db"; auto conn_deleter = [](PGconn* c) { PQfinish(c); }; auto res_deleter = [](PGresult* r) { PQclear(r); std::cout &lt;&lt; "deleted" &lt;&lt; std::endl; }; std::unique_ptr&lt;PGconn, decltype(conn_deleter)&gt; conn(PQconnectdb(conn_string), conn_deleter); if (PQstatus(conn.get()) != ConnStatusType::CONNECTION_OK) std::cerr &lt;&lt; "Problem" &lt;&lt; std::endl; std::vector&lt;const char *&gt; params{ "1" }; std::unique_ptr &lt; PGresult, decltype(res_deleter)&gt; res(PQexecParams(conn.get(), "SELECT * FROM table_with_double WHERE id = $1", params.size(), /* one param */ NULL, /* let the backend deduce param type */ (const char * const *)&amp;params.front(), NULL, /* don't need param lengths since text */ NULL, /* default to all text params */ 1), /* ask for binary results */ res_deleter); if (PQresultStatus(res.get()) != ExecStatusType::PGRES_TUPLES_OK) std::cout &lt;&lt; "SELECT failed: " &lt;&lt; PQerrorMessage(conn.get()) &lt;&lt; std::endl; show_binary_results(res.get()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload