Building a C/C++ application using embedded SQL - IBM DB2

The steps involved in building a C/C++ application with embedded SQL are:

  1. Create C/C++ source files containing programs with embedded SQL statements. The extension of a source file is dependent on the platform and host language (C or C++). The possible file extensions , “Precompiler source file extensions”
  2. Connect to the database, and precompile each source file using the DB2 PREP or PRECOMPILE command.
    Depending on the file extension, either the C or C++ precompiler is invoked. The precompiler generates modified source files containing C/C++ language calls for the SQL statements. Depending on the options specified, packages corresponding to the embedded SQL statements in the source files can be created in the database, or bind files containing information about how to create the packages can be produced.
  3. Compile the modified source files (and other files without SQL statements) using the C compiler.
  4. Using the object files created from the compilation, link the files with DB2 UDB and C libraries to produce an executable program.
  5. If packages were not created at precompile time (or if a different database is to be accessed) and bind files were created, the bind files are bound against the database.
  6. Run the application.

For our example, assume we finished our C inventory program named inventory.sqc on a Windows system using Visual C++. To precompile the program, we can run the following statement shown in Example

Example Output from running the PREP command

This will produce a modified source file named inventory.c and a package in the database.

Alternatively, we can create a bind file and later bind the file to manually create a package. This is known as deferred binding. We can accomplish this by using the BINDFILE option in the PRECOMPILE command.

Basic statements to create a bind file and package

After precompiling, we need to compile the modified source file. We can do this on the Windows system by running the command:

cl -Zi -Od -c -W2 inventory.c

This produces an object file named inventory.obj. We can then create an executable file named inventory.exe by running the command:

link -debug -out:inventory.exe inventory.obj db2api.lib

Depending on your compiler and the options you set, the commands for compiling and linking will be different.

Host variables and parameter markers
In a static SQL statement, the complete SQL statement along with the type and length of the data requested is known at precompile time. The only missing information is the actual data values for the SQL statement. Acting as placeholders for the missing data, host variables need to be defined by the application to represent the remaining information in the SQL statement. Host variables are represented by prefixing a colon (:) in front of the variable name.

For a dynamic SQL statement, parameter markers are represented as a question mark (?) for any variable information in an SQL statement.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM DB2 Topics