newbie libpq question...
newbie libpq question...
От:
Doug Homoelle <homoelle@ll.mit.edu>
Дата:
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
Re: newbie libpq question...
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Doug Homoelle 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
Re: newbie libpq question...
От:
"D'Arcy J.M. Cain" <darcy@druid.net>
Дата:
On Mon, 08 Nov 2004 17:36:09 -0500 Doug Homoelle 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 | 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.
Re: newbie libpq question...
От:
"Jeroen T. Vermeulen" <jtv@xs4all.nl>
Дата:
On Mon, Nov 08, 2004 at 06:17:31PM -0500, Tom Lane wrote: > Doug Homoelle 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
Re: newbie libpq question...
От:
Doug Homoelle <homoelle@ll.mit.edu>
Дата:
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.
Re: newbie libpq question...
От:
Edmund Bacon <ebacon@onesystem.com>
Дата:
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
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