Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cf9I1-00022E-9V for pgsql-bugs@arkaria.postgresql.org; Sat, 18 Feb 2017 17:59:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cf9I0-00047C-RN for pgsql-bugs@arkaria.postgresql.org; Sat, 18 Feb 2017 17:59:20 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cf9HL-0003Nc-Rq for pgsql-bugs@postgresql.org; Sat, 18 Feb 2017 17:58:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cf9HC-0003zj-8p for pgsql-bugs@postgresql.org; Sat, 18 Feb 2017 17:58:38 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v1IHwSwj027459; Sat, 18 Feb 2017 12:58:29 -0500 From: Tom Lane To: "Gao Yanxiao" <553216793@qq.com> cc: pgsql-bugs@postgresql.org Subject: Re: BUG? select count(*) from table don't get value via PQgetvalue() function in libpq(C) In-reply-to: <000001d28a00$a3549c30$e9fdd490$@qq.com> References: <000001d28a00$a3549c30$e9fdd490$@qq.com> Comments: In-reply-to "Gao Yanxiao" <553216793@qq.com> message dated "Sun, 19 Feb 2017 00:04:08 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27457.1487440708.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 18 Feb 2017 12:58:28 -0500 Message-ID: <27458.1487440708@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org "Gao Yanxiao" <553216793@qq.com> writes: > res =3D PQexecParams(pgconn, "select count(*) from chat_connstate", 0= , NULL, NULL, NULL, NULL, 1); So you asked for binary-format results ... > So, I use PQgetvalue() function get the value, like this: > Int result =3D *((int*)PQgetvalue(res, 0, 0)); > Or=20 > Long result =3D *((long*)PQgetvalue(res, 0, 0); The first of those is certainly not going to work, and depending on what platform you're on the second won't either, because it's only extracting the first 32 bits of the 64-bit result of count(). The other problem you've got here is that the result is coming off the wire in big-endian byte order, and you're not doing anything to convert that into native byte order (which more than likely is little-endian). The reason you're seeing zeroes is that the high 32 bits of the result are zeroes. Personally, I would not bother with binary format unless I anticipated processing enormous volumes of data; it's just too error-prone. Better to use text and apply strtol() or whatever. If you really must do it in binary format, here's the way pq_getmsgint64 reads a 64-bit big-endian value: int64 result; uint32 h32; uint32 l32; pq_copymsgbytes(msg, (char *) &h32, 4); pq_copymsgbytes(msg, (char *) &l32, 4); h32 =3D ntohl(h32); l32 =3D ntohl(l32); result =3D h32; result <<=3D 32; result |=3D l32; which is pretty tedious, but there's no standard 64-bit version of ntohl/htonl, so we have to swap each half separately. regards, tom lane --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs