114 DB2 UDB for Linux on iSeries: Implementation Guide
0070.00 querystring[0] = 0x00;
0071.00 strcat(querystring, "SELECT ID, NAME, DEPT, JOB, YEARS, SALARY FROM ");
0072.00 strcat(querystring, argv[2]);
0073.00 strcat(querystring, ".STAFF WHERE ID > 280");
0074.00 printf("Running SQL : %s\n", querystring);
0075.00 if ((sqe=SQLExecDirect(statement,querystring, SQL_NTS))) die2("exec direct",sqe);
0076.00
0077.00 /* Bind the buffers to the data to be fetched */
0078.00 if ((sqe=SQLBindCol(statement, 1, SQL_CHAR, field1,100, &retfld1))) die2("Bind field 1", sqe);
0079.00 if ((sqe=SQLBindCol(statement, 2, SQL_CHAR, field2,100, &retfld2))) die2("Bind field 2", sqe);
0080.00 if ((sqe=SQLBindCol(statement, 3, SQL_CHAR, field3,100, &retfld3))) die2("Bind field 3", sqe);
0081.00 if ((sqe=SQLBindCol(statement, 4, SQL_CHAR, field4,100, &retfld4))) die2("Bind field 4", sqe);
0082.00 if ((sqe=SQLBindCol(statement, 5, SQL_CHAR, field5,100, &retfld5))) die2("Bind field 5", sqe);
0083.00 if ((sqe=SQLBindCol(statement, 6, SQL_CHAR, field6,100, &retfld6))) die2("Bind field 6", sqe);
0084.00
0085.00
0086.00 /* The loop to fetch the all selected data */
0087.00 res = SQLFetch(statement);
0088.00 /*while (SQL_SUCCEEDED(res)) { */
0089.00 while ((((res)&(~1))==0)) { 1
0090.00
0091.00 /* Print the data with tab separated */
0092.00 printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",field1,field2,field3,field4,field5,field6);
0093.00 res= SQLFetch(statement); /* Next record */
0094.00 }
0095.00
0096.00 /*-------------------------------*/
0097.00 /* Clean up */
0098.00 /*-------------------------------*/
0099.00 /* Deallocate the statement handle */
0100.00 if (SQLFreeStmt(statement,SQL_DROP)) die("Free Statement with Drop");
0101.00
0102.00 /* Deallocate the connection handle */
0103.00 if ((sqe=SQLDisconnect(hdbc[0] ))) die2("Disconnect ",sqe);
0104.00 if ((sqe=SQLFreeConnect(hdbc[0]))) die2("Free Connection",sqe);
0105.00
0106.00 /* Deallocate the environment handle */
0107.00 if (SQLFreeEnv(henv)) die("Free Environment");
0108.00
0109.00 diehappy("Got to end");
0110.00
0111.00 return 0;
0112.00 }
0113.00
0114.00
0115.00 /* dieXXX() are the functions to print the ending status */
0116.00
0117.00 void diehappy(char *reason) {
0118.00 printf("%s\n",reason);
0119.00 }
0120.00
0121.00 void die(char *reason) {
0122.00 printf("Failure because: %s\n",reason);
0123.00 exit(1);
0124.00 }
0125.00
0126.00 void die2(char *reason,SQLRETURN code) {
0127.00 printf("Error code: %d\n",code);
0128.00 die(reason);
0129.00 }
0130.00
0131.00
****************** End of data *********************************************************************
Compile and run
To compile the TESTCLI program, simply use the CRTBNDC command.
CRTBNDC PGM(REDBOOK/TESTCLI) SRCFILE(REDBOOK/QCSRC)
Get DB2 for Linux on iSeries: Implementation Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.