ECPG - how to fetch then sort strings

From: Poul Jensen <flyvholm(at)gfy(dot)ku(dot)dk>
To: pgsql-general(at)postgresql(dot)org
Subject: ECPG - how to fetch then sort strings
Date: 2006-09-08 13:55:44
Message-ID: 45017660.3060001@gfy.ku.dk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I need to fetch strings from a database with ECPG and then sort them in
C. Here is one of my failed attempts:

###########################

int main(int argc, char *argv[]) {

int maxlen=20;
long nrec;

EXEC SQL BEGIN DECLARE SECTION;
varchar filenms[][maxlen]=NULL;
char dbnm[50];
EXEC SQL END DECLARE SECTION;

sprintf(dbnm,"%s",argv[1]);

EXEC SQL CONNECT TO :dbnm;

EXEC SQL SELECT filenm INTO :filenms FROM beamdata;
nrec = sqlca.sqlerrd[2]; /* Returns number of rows retrieved */

EXEC SQL COMMIT;
EXEC SQL DISCONNECT;

qsort(filenms, nrec, maxlen*sizeof(char), scmp);

free(filenms);

return 0;

}

static int scmp( const void *sp1, const void *sp2 )
{
return( strcmp(*(char **)sp1, *(char **)sp2) );
}

###########################

It compiles ok, but I get garbage in variable filenms. If I change the
declaration of filenms to:
char **filenms=NULL;
the SQL query returns strings ok. But the strings have variable length,
and I need to specify one length in qsort, so it won't work. Another
attempt to try to specify string length:
char (*filenms)[maxlen]=NULL;
Won't compile, ECPG doesn't accept this syntax. Well, and strcmp crashes
(segmentation fault) in function scmp regardless what I try. Not SQL
error, I know, but if anybody can tell why I'd be grateful.

Any suggestions?

Thanks,
Poul Jensen

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2006-09-08 14:01:44 Re: PostgreSQL books for beginner
Previous Message A. Kretschmer 2006-09-08 13:45:50 Re: PostgreSQL books for beginner