pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gao Yanxiao <553216793@qq.com>
To: pgsql-bugs@postgresql.org
Subject: BUG?  select count(*) from table   don't get value via PQgetvalue() function in libpq(C)
Date: Sun, 19 Feb 2017 00:04:08 +0800
Message-ID: <000001d28a00$a3549c30$e9fdd490$@qq.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Hi, postgresql:

       First, my englist is not good. So I try to my best. Hope you can understand me.

 

       I programing a program use linux via C language. And I used libpq connect to postgresql.

       Then I want calculate rows in a table. So I write PQexecparams() perfrom sql statement: select count(*) from tablename. 

Like this:

    PGresult *res;

    PQtrace(pgconn, stdout);

    res = PQexecParams(pgconn, "select count(*) from chat_connstate", 0, NULL, NULL, NULL, NULL, 1);

 

Then I check res variable use 

PQresultStatus(res) == PGRES_TUPLES_OK

       that the status is successed.

 

So, I use PQgetvalue() function get the value, like this:

       Int result = *((int*)PQgetvalue(res, 0, 0));

       Or 

       Long result = *((long*)PQgetvalue(res, 0, 0);

But, the result variable value is zero, the I use PQfname(res, 0) and PQfnumber(res, “count”) check status that output is corrected.

So, I change my code to:

       Int result = *((int*)PQgetvalue(res, 0, PQfnumber(res, “count”);

But, the result value  still is zero.

 

I login postgresql use psql, then perfrom select count(*) from tablename. The output is current, like:

       chatdb=# select count(*) from chat_connstate;

       count 

-------

          1

(1 行记录)

  

Then I perfrom “select * from tablename” in PQexecparams, and get the values. The values is currect this time.

 

So, I test 

       Select sum(*) from tablename;

       Int result = *((int*)PQgetvalue(res, 0, 0);   //the result value is zero

       

       Select avg(*) from tablename;

       Double result = *((double*)PQgetvalue(res, 0, 0); // the result value is not zero, but still is not equal to psql output.

 

Finally, I have no ideas for new test way. Please help me.

Thanks.

 

 

output infomations use psql and PQtrace() function in gdb:  (The system and postgresql environment in the bottom.)

 

chatdb=# select * from chat_connstate;

id |                       ip                       | port | connfd | account |                  token                   |          expires           |        create_date         

----+------------------------------------------------+------+--------+---------+------------------------------------------+----------------------------+----------------------------

  1 | 127.0.0.1                                      |    1 |      7 |         | 34a57ff275715c87235bf880f4c642d2539372fc | 2017-02-18 22:33:55.381506 | 2017-02-18 22:33:55.381506

(1 行记录)

 

1. Select count(*) from chat_connstate

 

(gdb) 

41       chat_psql_init_connstate(1);

(gdb) s

chat_psql_init_connstate (clear=1) at chat_psql.c:38

38       PQtrace(pgconn, stdout);

(gdb) n

39       res = PQexecParams(pgconn, "select count(*) from chat_connstate",

(gdb)

To backend> Msg P

To backend> ""

To backend> "select count(*) from chat_connstate"

To backend (2#)> 0

To backend> Msg complete, length 44

To backend> Msg B

To backend> ""

To backend> ""

To backend (2#)> 0

To backend (2#)> 0

To backend (2#)> 1

To backend (2#)> 1

To backend> Msg complete, length 15

To backend> Msg D

To backend> P

To backend> ""

To backend> Msg complete, length 7

To backend> Msg E

To backend> ""

To backend (4#)> 0

To backend> Msg complete, length 10

To backend> Msg S

To backend> Msg complete, length 5

view thread (2+ messages)  latest in thread

Message-ID: <000001d28a00$a3549c30$e9fdd490$@qq.com>
Permalink:  ../000001d28a00$a3549c30$e9fdd490$@qq.com/
Also on:    postgresql.org/message-id/000001d28a00$a3549c30$e9fdd490$@qq.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: 553216793@qq.com
  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>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox