Skip site navigation (1) Skip section navigation (2)

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: (view raw, whole thread or download thread mbox)
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,

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>

	PGconn *conn;
	PGresult  *res;
	int nf;
	int i, j, k;

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

	nf = PQnfields(res);

		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);
		printf("i = %d key = %d c = %d",i,*key,*count);
			printf(" elem a[%d] = %d, b[%d]",j,a[j],j,b[j]);

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

Get Your Private, Free E-mail from Indiatimes at

 Buy Music, Video, CD-ROM, Audio-Books and Music Accessories from

pgsql-interfaces by date

Next:From: Hannu KrosingDate: 2001-10-24 17:09:08
Subject: Re: Python interface and Money?
Previous:From: Thomas LockhartDate: 2001-10-24 13:44:35
Subject: Re: Python interface and Money?

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group