|
/*----------------------------------------------------------------------------
EXAMPLE of an embedded SQL C Program for DB2.
Connect to database GO3421 for this regardless of your section.
This APP takes one argument on the command line, a sailor's SID. It
then finds the sailor SID's age out of the table TECHSTU.SAILOR (in
database GO3421) and reports it. A dumb and not very interesting APP,
but it shows how things are done.
Written by D. Zilio
Tweaked by P. Godfrey
NOV 1999
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlcodes.h>
#include <sys/time.h>
#define EXIT 0
#define NOEXIT 1
/*----------------------------------------------------------------------------
Include DB2's SQL error reporting facility.
----------------------------------------------------------------------------*/
EXEC SQL INCLUDE SQLCA ;
/*----------------------------------------------------------------------------
Declare the SQL interface variables.
----------------------------------------------------------------------------*/
EXEC SQL BEGIN DECLARE SECTION ;
short sage;
short sid;
char sname[16];
EXEC SQL END DECLARE SECTION ;
/*----------------------------------------------------------------------------
Declare variables to be used in the following C program.
----------------------------------------------------------------------------*/
char msg[1025];
int rc;
int errcount;
/*----------------------------------------------------------------------------
This macro prints the message in the SQLCA if the return code is 0
and the SQLCODE is not 0.
----------------------------------------------------------------------------*/
#define PRINT_MESSAGE() \
{ \
if (rc == 0 && sqlca.sqlcode != 0) \
{ \
sqlaintp(msg, 1024, 0, &sqlca); \
printf("%s\n",msg); \
} \
}
/*----------------------------------------------------------------------------
This macro prints out all feilds in the SQLCA.
----------------------------------------------------------------------------*/
#define DUMP_SQLCA() \
{ \
printf("******************** DUMP OF SQLCA ********************\n"); \
printf("SQLCAID: %s\n", sqlca.sqlcaid); \
printf("SQLCABC: %d\n", sqlca.sqlcabc); \
printf("SQLCODE: %d\n", sqlca.sqlcode); \
printf("SQLERRML: %d\n", sqlca.sqlerrml); \
printf("SQLERRMC: %s\n", sqlca.sqlerrmc); \
printf("SQLERRP: %s\n", sqlca.sqlerrp); \
printf("SQLERRD[0]: %d\n", sqlca.sqlerrd[0]); \
printf("SQLERRD[1]: %d\n", sqlca.sqlerrd[1]); \
printf("SQLERRD[2]: %d\n", sqlca.sqlerrd[2]); \
printf("SQLERRD[3]: %d\n", sqlca.sqlerrd[3]); \
printf("SQLERRD[4]: %d\n", sqlca.sqlerrd[4]); \
printf("SQLERRD[5]: %d\n", sqlca.sqlerrd[5]); \
printf("SQLWARN: %s\n", sqlca.sqlwarn); \
printf("SQLSTATE: %s\n", sqlca.sqlstate); \
printf("****************** END OF SQLCA DUMP *******************\n"); \
}
/*----------------------------------------------------------------------------
This macro prints the message in the SQLCA if one exists. If the
return code is not 0 or the SQLCODE is not expected, an error occurred
and must be recorded.
----------------------------------------------------------------------------*/
#define CHECK_SQL(code,text_string,eExit) \
{ \
PRINT_MESSAGE(); \
if (rc != 0 || sqlca.sqlcode != code) { \
printf("%s\n",text_string); \
printf("Expected code = %d\n",code); \
if (rc == 0) { \
DUMP_SQLCA(); \
} \
else printf("RC: %d\n",rc); \
errcount += 1; \
if (eExit == EXIT) goto errorexit; \
} \
}
/*----------------------------------------------------------------------------
The PROGRAM.
----------------------------------------------------------------------------*/
main (int argc, char *argv[])
{
/* Grab the first command argument. This is the SID. */
if (argc > 1) {
sid = atoi(argv[1]);
printf("SID requested is %d.\n", sid);
/* If there is no arguement, bail. */
} else {
printf("Which SID?\n");
exit(0);
}
EXEC SQL CONNECT TO GO3421;
CHECK_SQL(0, "Connect failed", EXIT);
/* Find the name and age of sailor SID. */
EXEC SQL SELECT SNAME, AGE into :sname, :sage
FROM TECHSTU.SAILOR
WHERE sid = :sid;
CHECK_SQL(0, "The SELECT query failed.", EXIT);
/* Report the age. */
printf("Sailor %s's age is %d.\n", sname, sage);
printf("Executed Successfuly\n") ;
printf("Bye\n") ;
errorexit:
EXEC SQL CONNECT RESET;
}
|
|