variable length arrays (fields) - Mailing list pgsql-interfaces

From nitinpdhavale
Subject variable length arrays (fields)
Date
Msg-id 200110241531.VAA26015@WS0005.indiatimes.com
Whole thread Raw
List 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
CURSORFOR 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



pgsql-interfaces by date:

Previous
From: Christof Petig
Date:
Subject: ECPG: Automatic Storage allocation for NULL-pointing output variables
Next
From: Thomas Lockhart
Date:
Subject: Re: Python interface and Money?