ECPG - how to fetch then sort strings - Mailing list pgsql-general

From Poul Jensen
Subject ECPG - how to fetch then sort strings
Date
Msg-id 45017660.3060001@gfy.ku.dk
Whole thread Raw
In response to SQL - planet redundant data  (Poul Jensen <flyvholm@gfy.ku.dk>)
Responses Re: ECPG - how to fetch then sort strings
List 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

pgsql-general by date:

Previous
From: "Brandon Aiken"
Date:
Subject: Re: Database design and triggers...
Next
From: Tom Lane
Date:
Subject: Re: Query performance inconsistant.