pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
newbie libpq question...
8+ messages / 6 participants
[nested] [flat]

* newbie libpq question...
@ 2004-11-08 22:23  Doug Homoelle <homoelle@ll.mit.edu>
  0 siblings, 0 replies; 8+ messages in thread

From: Doug Homoelle @ 2004-11-08 22:23 UTC (permalink / raw)
  To: pgsql-interfaces

I'm trying to use the libpq library for the first time and I'm
experiencing what seems to be a very basic problem.  When I run the
following:

	res = PQexec(conn, "select blah blah blah");
	test_int=PQntuples(res);
	fprintf(stdout,"number of rows: %f\n",test_int);
	test_int=PQnfields(res);
	fprintf(stdout,"number of cols: %f\n",test_int);

where "blah blah blah" is the selection criteria, I get zero rows and
zero columns.  When I do the exact same "select" query through the psql
interface I get 547 rows and 3 columns.  I've checked that
PQresultStatus = PGRES_TUPLES_OK, so it seems like it completed the
command correctly.  I'm new to postgres and I haven't used C in a long
while so I'm a little rusty there.  Am I missing something obvious?

thanks in advance for any insight,
dh




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* newbie libpq question...
@ 2004-11-08 22:36  Doug Homoelle <homoelle@ll.mit.edu>
  0 siblings, 1 reply; 8+ messages in thread

From: Doug Homoelle @ 2004-11-08 22:36 UTC (permalink / raw)
  To: pgsql-interfaces

I'm trying to use the libpq library for the first time and I'm
experiencing what seems to be a very basic problem.  When I run the
following:

        res = PQexec(conn, "select blah blah blah");
        test_int=PQntuples(res);
        fprintf(stdout,"number of rows: %f\n",test_int);
        test_int=PQnfields(res);
        fprintf(stdout,"number of cols: %f\n",test_int);

where "blah blah blah" is the selection criteria, I get zero rows and
zero columns.  When I do the exact same "select" query through the psql
interface I get 547 rows and 3 columns.  I've checked that
PQresultStatus = PGRES_TUPLES_OK, so it seems like it completed the
command correctly.  I'm new to postgres and I haven't used C in a long
while so I'm a little rusty there.  Am I missing something obvious?

thanks in advance for any insight,
dh




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: newbie libpq question...
@ 2004-11-08 23:17  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2004-11-08 23:17 UTC (permalink / raw)
  To: Doug Homoelle <homoelle@ll.mit.edu>; +Cc: pgsql-interfaces

Doug Homoelle <homoelle@ll.mit.edu> writes:
>         res = PQexec(conn, "select blah blah blah");
>         test_int=PQntuples(res);
>         fprintf(stdout,"number of rows: %f\n",test_int);
>         test_int=PQnfields(res);
>         fprintf(stdout,"number of cols: %f\n",test_int);

If test_int is an integer, you want %d or %i in the format, not %f ...

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: newbie libpq question...
@ 2004-11-08 23:21  D'Arcy J.M. Cain <darcy@druid.net>
  parent: Doug Homoelle <homoelle@ll.mit.edu>
  0 siblings, 2 replies; 8+ messages in thread

From: D'Arcy J.M. Cain @ 2004-11-08 23:21 UTC (permalink / raw)
  To: Doug Homoelle <homoelle@ll.mit.edu>; +Cc: pgsql-interfaces

On Mon, 08 Nov 2004 17:36:09 -0500
Doug Homoelle <homoelle@ll.mit.edu> wrote:
> I'm trying to use the libpq library for the first time and I'm
> experiencing what seems to be a very basic problem.  When I run the
> following:
> 
>         res = PQexec(conn, "select blah blah blah");
>         test_int=PQntuples(res);
>         fprintf(stdout,"number of rows: %f\n",test_int);
>         test_int=PQnfields(res);
>         fprintf(stdout,"number of cols: %f\n",test_int);
> 
> where "blah blah blah" is the selection criteria, I get zero rows and

I think you need "number of cols: %d\n" there.  Those functions returns
int.

-- 
D'Arcy J.M. Cain <darcy@druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: newbie libpq question...
@ 2004-11-09 14:32  Jeroen T. Vermeulen <jtv@xs4all.nl>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 8+ messages in thread

From: Jeroen T. Vermeulen @ 2004-11-09 14:32 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Doug Homoelle <homoelle@ll.mit.edu>; pgsql-interfaces

On Mon, Nov 08, 2004 at 06:17:31PM -0500, Tom Lane wrote:
> Doug Homoelle <homoelle@ll.mit.edu> writes:
 
> If test_int is an integer, you want %d or %i in the format, not %f ...

Plus, the singular for "criteria" is "criterion."  :-P




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: newbie libpq question...
@ 2004-11-09 14:54  Doug Homoelle <homoelle@ll.mit.edu>
  parent: D'Arcy J.M. Cain <darcy@druid.net>
  1 sibling, 0 replies; 8+ messages in thread

From: Doug Homoelle @ 2004-11-09 14:54 UTC (permalink / raw)
  To: D'Arcy J.M. Cain <darcy@druid.net>; +Cc: pgsql-interfaces

That worked.  I guess I would have expected an error if there was a type
mismatch, but like I said, my C is pretty rusty.

> I think you need "number of cols: %d\n" there.  Those functions returns
> int.




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: newbie libpq question...
@ 2004-11-09 15:46  Edmund Bacon <ebacon@onesystem.com>
  parent: D'Arcy J.M. Cain <darcy@druid.net>
  1 sibling, 0 replies; 8+ messages in thread

From: Edmund Bacon @ 2004-11-09 15:46 UTC (permalink / raw)
  To: pgsql-interfaces; +Cc: Doug Homoelle <homoelle@ll.mit.edu>

Doug Homoelle wrote:
> That worked.  I guess I would have expected an error if there was a type
> mismatch, but like I said, my C is pretty rusty.
> 

If you are using gcc you can add -Wformat to your compile options to 
have it report if your printf() arguments don't match the format 
specifier - I don't know if other compilers do this, or how to turn it 
on if they do.

Note that for the compiler to know if your printf() arguments are 
correct, you have to supply the format string as a string literal to the 
call.  As an example, see the following program:


$ cat print.c

#include <stdio.h>

int main(void)
{
     const char *format = "d = %f\n";
     int d = 0;

     printf(format, d);

     d = 1;
     printf("d = %f\n", d);

     return 0;
}

$ gcc -Wall -W print.c -o print
print.c: In function `main':
print.c:11: warning: double format, different type arg (arg 2)



-- 
Edmund Bacon <ebacon@onesystem.com>



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: newbie libpq question...
@ 2004-11-14 01:23  Michael Fuhr <mike@fuhr.org>
  0 siblings, 0 replies; 8+ messages in thread

From: Michael Fuhr @ 2004-11-14 01:23 UTC (permalink / raw)
  To: Doug Homoelle <homoelle@ll.mit.edu>; +Cc: pgsql-interfaces

On Mon, Nov 08, 2004 at 05:23:59PM -0500, Doug Homoelle wrote:
> 
> 	res = PQexec(conn, "select blah blah blah");
> 	test_int=PQntuples(res);
> 	fprintf(stdout,"number of rows: %f\n",test_int);
> 	test_int=PQnfields(res);
> 	fprintf(stdout,"number of cols: %f\n",test_int);
> 
> where "blah blah blah" is the selection criteria, I get zero rows and
> zero columns.

You're using floating-point (%f) conversions to print integers; use
%d or %i instead.  Some compilers have options to warn you of such
mistakes -- with gcc, for example, you can use -Wall.

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2004-11-14 01:23 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-11-08 22:23 newbie libpq question... Doug Homoelle <homoelle@ll.mit.edu>
2004-11-08 22:36 newbie libpq question... Doug Homoelle <homoelle@ll.mit.edu>
2004-11-08 23:21 ` D'Arcy J.M. Cain <darcy@druid.net>
2004-11-09 14:54   ` Doug Homoelle <homoelle@ll.mit.edu>
2004-11-09 15:46   ` Edmund Bacon <ebacon@onesystem.com>
2004-11-08 23:17 Re: newbie libpq question... Tom Lane <tgl@sss.pgh.pa.us>
2004-11-09 14:32 ` Jeroen T. Vermeulen <jtv@xs4all.nl>
2004-11-14 01:23 Re: newbie libpq question... Michael Fuhr <mike@fuhr.org>

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