Kbase 20755: Sample ESQL/C Program for Progress SQL-92 on UNIX and Linux
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  10/16/2008 |
|
SUMMARY:
This Knowledge Base Solution provides a sample C program to access Progress SQL-92 via the ESQL/C interface.
Additional ESQL/C examples using static and dynamic statements are available in the documentation: Embedded SQL-92 Guide and Reference.
To access the Progress documentation online you can visit:
http://www.progress.com/v9/documentation/
SOLUTION:
The C program is as follows:
#include <stdio.h>
#include <string.h>
#include <time.h>
void sqlquery(char *);
int etime(int);
struct sqlca sqlca;
dh_i32_t SQLCODE;
int main()
{
EXEC SQL BEGIN DECLARE SECTION ;
char connect_string[120];
EXEC SQL END DECLARE SECTION ;
strncpy(connect_string,
"progress:T:localhost:2500:sports2000",
119);
EXEC SQL WHENEVER SQLERROR GOTO mainerr ;
EXEC SQL CONNECT TO :connect_string AS 'conn' ;
sqlquery("select name from pub.customer");
EXEC SQL DISCONNECT 'conn';
exit(0);
mainerr:
fprintf(stderr,
"main: SQL Error (%ld) %s\n",
sqlca.sqlcode,
sqlca.sqlerrm);
exit(1);
}
void sqlquery(char * s)
{
EXEC SQL BEGIN DECLARE SECTION ;
char sql_stmt[256];
char field1[256];
EXEC SQL END DECLARE SECTION ;
strncpy(sql_stmt, s, 255);
EXEC SQL WHENEVER SQLERROR GOTO sqlerr ;
EXEC SQL PREPARE stmtid FROM :sql_stmt ;
EXEC SQL DECLARE x CURSOR FOR stmtid ;
EXEC SQL OPEN x ;
EXEC SQL WHENEVER NOT FOUND GOTO sqldone ;
etime(1);
while(1){
EXEC SQL FETCH x INTO :field1 ;
printf("%s\n", field1);
}
sqldone:
printf("Etime: %d secs\n", etime(0));
EXEC SQL CLOSE x ;
EXEC SQL COMMIT WORK ;
return;
sqlerr:
fprintf(stderr,
"sqlquery: SQL Error (%ld) %s\n",
sqlca.sqlcode, sqlca.sqlerrm);
exit(1);
}
int etime(int set)
{
static time_t t1 = 0;
int r;
r = time(0) - t1;
if (set) t1 = time(0);
return r;
}