Обсуждение: How to retrieve values of 'array' field using C library (libpq)

Поиск
Список
Период
Сортировка

How to retrieve values of 'array' field using C library (libpq)

От
Anton Maksimenkov
Дата:
Hi.

I use libpq in my program. And I can retrieve int and symbolic fields
as simple as


int unameFN, moneyFN...
char *unamePTR, *moneyPTR...

unameFN = PQfnumber(res, "uname");
moneyFN = PQfnumber(res, "money");
unamePTR = PQgetvalue(res, 0, unameFN);
moneyPTR = PQgetvalue(res, 0, moneyFN);
themoney   = ntohl(*((uint32_t *) moneyPTR));
printf("user->[uname:%s money:%d]", unamePTR, themoney);


Then I tried to use array type and no success:

int arFN...
char *arPTR...

arN = PQfnumber(res, "woo");
arPTR = PQgetvalue(res, 0, arFN);
printf("user->[woo:%s]", arPTR);

I can convert it with array_to_string() and got it as some text.

My array is the array of integer values. So maybe there is some native
method to retrieve them (as in the 'themoney' example)?
--
antonvm

Re: How to retrieve values of 'array' field using C library (libpq)

От
Dmitriy Igrishin
Дата:
Hey Anton,

2011/2/9 Anton Maksimenkov <anton200@gmail.com>
Hi.

I use libpq in my program. And I can retrieve int and symbolic fields
as simple as


int unameFN, moneyFN...
char *unamePTR, *moneyPTR...

unameFN = PQfnumber(res, "uname");
moneyFN = PQfnumber(res, "money");
unamePTR = PQgetvalue(res, 0, unameFN);
moneyPTR = PQgetvalue(res, 0, moneyFN);
themoney   = ntohl(*((uint32_t *) moneyPTR));
printf("user->[uname:%s money:%d]", unamePTR, themoney);


Then I tried to use array type and no success:

int arFN...
char *arPTR...

arN = PQfnumber(res, "woo");
arPTR = PQgetvalue(res, 0, arFN);
printf("user->[woo:%s]", arPTR);

I can convert it with array_to_string() and got it as some text.

My array is the array of integer values. So maybe there is some native
method to retrieve them (as in the 'themoney' example)?
Not in libpq. Probably, in libpqtypes.
--
antonvm

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



--
// Dmitriy.


Re: How to retrieve values of 'array' field using C library (libpq)

От
Joshua Tolley
Дата:
On Wed, Feb 09, 2011 at 02:53:37PM +0300, Dmitriy Igrishin wrote:
> > My array is the array of integer values. So maybe there is some native
> > method to retrieve them (as in the 'themoney' example)?
> >
> Not in libpq. Probably, in libpqtypes.

I ended up writing one for pgsnmpd, which you're welcome to use. I can't
guarantee that writing my own was the best way to do it, or that it's free of
bugs. See pg_array.c and pg_array.h at
http://git.postgresql.org/gitweb?p=pgsnmpd.git;a=summary

--
Joshua Tolley / eggyknap
End Point Corporation
http://www.endpoint.com

Вложения