Download DB2 Express-C: The Developer Handbook for XML
Transcript
4.5.6 Complete CLI Inventory Program
In our example CLI inventory program (Example 4-34), we connect to the
database, prepare a SELECT statement, and then bind the parameter marker.
Because QUANTITY is of type INTEGER in the database, we bind the parameter
marker with a C type of SQL_C_LONG and SQL type of SQL_INTEGER. In
binding a parameter marker, the SQL data type and the symbolic C data type
need to be known as input to the CLI API.
We also call SQLBindCol() for each column of the result set. Because QUANTITY
and PID data values are returned from the INVENTORY table, we call
SQLBindCol() to bind both columns to application buffers. Once we finish
processing the result set, we disconnect from the database.
For simplicity and ease of readability, we have omitted error checking in our
example CLI application.
Example 4-34 CLIinventory.c
/******************************************************
** Source File Name: CLIinventory.c
**
** This simple CLI program will print a list of
** products that need to be restocked.
** The user needs to supply the minimum acceptable
** quantity value before a product is considered to be
** low in stock.
**
*******************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <sqlcli.h>
int main(int argc, char *argv[]) {
SQLHANDLE env_handle;
SQLHANDLE conn_handle;
SQLHANDLE stmt_handle;
SQLCHAR PID[11];
SQLINTEGER quantity;
SQLLEN ind[2];
SQLINTEGER min_quality=atoi(argv[1]);
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env_handle);
SQLAllocHandle(SQL_HANDLE_DBC, env_handle, &conn_handle);
188
DB2 Express-C: The Developer Handbook for XML, PHP, C/C++, Java, and .NET