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

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

              http://www.postgresql.org/docs/faq

Reply via email to