variable length arrays (fields)

From: "nitinpdhavale" <nitinpdhavale(at)indiatimes(dot)com>
To: <pgsql-interfaces(at)postgresql(dot)org>
Subject: variable length arrays (fields)
Date: 2001-10-24 15:13:10
Message-ID: 200110241531.VAA26015@WS0005.indiatimes.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces


Hello all,
I am having a problem accessing variable length
arrays ( or rather fields ) in postgres tables from
a C program. Can somebody please help me out ?
I've included the source files below. I've have not included
the error checks (CONNECTION_BAD etc).
Thanks in advance,
-npd

file test.sql
---------------

create table temp (key int4, count int4, a int4[], b int4[]);

insert into temp values( 1, 2, '{1,2}', '{3,4}' );
insert into temp values( 2, 1, '{1}', '{2}' );
insert into temp values( 3, 3, '{1,2,3}', '{3,4,5}' );

file test.c
-------------

include <stdio.h>
#include <pgsql/libpq-fe.h>

main()
{
PGconn *conn;
PGresult *res;
int nf;
int i, j, k;

conn = PQsetdb(NULL,NULL,NULL,NULL,"test");
res = PQexec(conn,"BEGIN");
PQclear(res);
res = PQexec(conn,"DECLARE mc BINARY CURSOR FOR select * from temp");
PQclear(res);
res = PQexec(conn,"FETCH ALL in mc");
PQclear(res);

nf = PQnfields(res);

for(i=0;i<PQntuples(res);++i)
{
int *key, *count;
int *a, *b, la, lb;

key = PQgetvalue(res,i,0);
count = PQgetvalue(res,i,1);
la = PQgetlength(res,i,2);
lb = PQgetlength(res,i,3);
a = (int *) malloc(la);
b = (int *) malloc(lb);
memmove(a,PQgetvalue(res,i,3),la);
printf("i = %d key = %d c = %d",i,*key,*count);
for(j=0;j<*count;++j)
{
printf(" elem a[%d] = %d, b[%d]",j,a[j],j,b[j]);
}
printf("\n");
}

PQclear(res);

res = PQexec(conn,"CLOSE mc"); PQclear(res);

res = PQexec(conn,"COMMIT"); PQclear(res);

PQfinish(conn);
}

Get Your Private, Free E-mail from Indiatimes at http://email.indiatimes.com

Buy Music, Video, CD-ROM, Audio-Books and Music Accessories from http://www.planetm.co.in

Browse pgsql-interfaces by date

  From Date Subject
Next Message Hannu Krosing 2001-10-24 17:09:08 Re: Python interface and Money?
Previous Message Thomas Lockhart 2001-10-24 13:44:35 Re: Python interface and Money?