IBM Support

From source code to execution: Embedded SQL statements in C application

Technical Blog Post


Abstract

From source code to execution: Embedded SQL statements in C application

Body

In the knowledge center, we have all commands for Embedded SQL programming in C applicationand even sample code(ref 1), but I could not find out an example which shows whole process from source to execution. Therefore I am writing this article to show the whole process for the sqc programming. You can glance over the steps quickly and then see the actual commands and the results.
----------------------------------------
1. source code : tst.sqc
2. precompile and compile : tst.sqc -> tst.c -> tst
3. execution.
----------------------------------------
 
1. source code : tst.sqc
cat tst.sqc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sql.h>
#include <sqlda.h>
#include <sqlca.h>
#include <sqlenv.h>
#include <sqlcodes.h>
#include <sqlutil.h>
 
EXEC SQL INCLUDE SQLCA;
 
EXEC SQL BEGIN DECLARE SECTION;
  char H00025[18];
 
EXEC SQL END DECLARE SECTION;
 
/* methods to perform SELECT */
int TbSelectUsingFetchIntoHostVariables(void);
 
int main(int argc, char *argv[])
{
  int rc = 0;
  char dbAlias[SQL_ALIAS_SZ + 1];
 
  /* connect to database */
  EXEC SQL CONNECT TO TSTV111;
  if (sqlca.sqlcode < 0)
  {
    printf(" Failed to connect \n");
    return 1;
  }
 
  /* methods to perform SELECT */
  rc = TbSelectUsingFetchIntoHostVariables();
 
  /* disconnect from the database */
  EXEC SQL CONNECT RESET;
  if (sqlca.sqlcode < 0)
  {
    printf(" Failed to connect \n");
    return 1;
  }
 
  return 0;
} /* main */
 
int TbSelectUsingFetchIntoHostVariables(void)
{
  int rc = 0;
  struct sqlca sqlca;
 
  /* declare cursor */
  EXEC SQL SELECT 'TEST' INTO :H00025 FROM SYSIBM.SYSDUMMY1;
 
  if (sqlca.sqlcode < 0)
  {
    printf(" Failed to select \n");
    return 1;
  }
 
  printf("Dummy data from SYSIBM.SYSDUMMY1 : %s\n", H00025);
  printf("SELECT 'TEST' INTO :H00025 FROM SYSIBM.SYSDUMMY1; was done\n");
  return 0;
} /* TbSelectUsingFetchIntoHostVariables */
 
 
2. precompile and compile : tst.sqc -> tst.c -> tst
+ db2 connect to tstv111
 
   Database Connection Information
 
 Database server        = DB2/AIX64 11.1.1.1
 SQL authorization ID   = WOONGC
 Local database alias   = TSTV111
 
+ db2 prep tst.sqc
 
LINE    MESSAGES FOR tst.sqc
------  --------------------------------------------------------------------
        SQL0060W  The "C" precompiler is in progress.
        SQL0091W  Precompilation or binding was ended with "0"
                  errors and "0" warnings.
+ cc -q64 -I/home/woongc/sqllib/include -c tst.c
+ cc -q64 -o tst tst.o -L/home/woongc/sqllib/lib64 -ldb2
+ db2 terminate
DB20000I  The TERMINATE command completed successfully.
 
 
3. execution
$ ./tst
Dummy data from SYSIBM.SYSDUMMY1 : TEST
SELECT 'TEST' INTO :H00025 FROM SYSIBM.SYSDUMMY1; was done
 
-----------------------------------
ref 1
 
Embedded SQL statements in C and C++ applications
 
Precompilation of embedded SQL applications with the PRECOMPILE command
 
AIX C embedded SQL and DB2 API applications compile and link options
 
Embedded SQL application template in C
-----------------------------------

[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SSEPGG","label":"Db2 for Linux, UNIX and Windows"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

UID

ibm13286461