Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

ODBC API Basics

Before you get into the various functions provided by the ODBC API, let's look at a few key concepts used throughout the API. In this section, you will look at allocating various handles that are used by ODBC, data types used in ODBC, and how ODBC passes error information to your application.

ODBC Handles

The ODBC API introduces new handle types that are used to reference information about your app's ODBC environment, specific database connections, SQL statements, and Descriptors. In ODBC 3, each of these handle types is allocated with a single function—SQLAllocHandle()—and freed with a single function—SQLFreeHandle().

The SQLAllocHandle() function allocates the internal structures for the various handle types:

SQLRETURN SQLAllocHandle(SQL_SMALLINT HandleType, SQLHANDLE InputHandle,
    SQLHANDLE * OutputHandlePtr);

The HandleType parameter can be one of the following values:

These values determine which sort of handle is being allocated. You will look at specific examples of allocating each type when you learn about allocating environment, connection, statement, and descriptor handles.

The InputHandle specifies the handle from which the new handle is derived. For example, a connection handle is allocated based on an environment handle. An environment handle, however, isn't derived from another handle, so InputHandle should be set to SQL_NULL_HANDLE.

The OutputHandlePtr should point to the new handle that is to be allocated.

If the new handle is successfully allocated, SQlAllocHandle() will return SQL_SUCCESS; otherwise, it will return SQL_ERROR. You will look at how to handle ODBC errors in just a bit.

When you are finished with a particular ODBC handle in your application, you should free the structures associated with the handle by calling SQLFreeHandle(), which takes the type of handle to free and the actual handle that is to be freed.

ODBC Data Types

ODBC defines many different standard data types that are used in ODBC applications. There are C data types, which are used in your application code, and SQL data types, which are used to describe the type of data that is used within a data source. Many of the ODBC calls that move data from the application to the data source, or vice versa, can automatically perform conversion between many of these types.

Some of the most common C data types defined in ODBC are listed in Table 20.1, although there are many others described in the ODBC 3 Programmer's Reference. The C type identifier is a constant used to represent the type, the ODBC typedef gives the type you should use in declaring this type of variable, and the C type shows what the preprocessor will resolve this to.

Table 20.1. ODBC C Data Types

C Type Identifier ODBC C Typedef C Type
SQL_C_CHAR SQLCHAR * unsigned char *
SQL_C_SSHORT SQLSMALLINT short int
SQL_C_USHORT SQLUSMALLINT unsigned short int
SQL_C_SLONG SQLINTEGER long int
SQL_C_ULONG SQLUINTEGER unsigned long int
SQL_C_FLOAT SQLREAL float
SQL_C_DOUBLE SQLDOUBLE double
SQL_C_BIT SQLCHAR unsigned char
SQL_C_STINYINT SQLSCHAR signed char
SQL_C_UTINYINT SQLCHAR unsigned char
SQL_C_SBIGINT SQLBIGINT _int64
SQL_C_UBIGINT SQLUBIGINT unsigned _int64
SQL_C_BINARY SQLCHAR * unsigned char *
SQL_C_VARBOOKMARK SQLCHAR * unsigned char *

ODBC uses SQL data types to describe the data types that are stored in the database. Table 20.2 shows some of the more common types, listing the constant SQL type identifier that is used in your apps and an example SQL definition for the type. For a complete listing, see the ODBC 3 Programmer's Reference.

Table 20.2. ODBC SQL Data Types

SQL Type Identifier Sample SQL Definition
SQL_CHAR CHAR(n)
SQL_VARCHAR VARCHAR(n)
SQL_LONGVARCHAR LONG VARCHAR
SQL_DECIMAL DECIMAL(p,s)
SQL_NUMERIC NUMERIC(p,s)
SQL_SMALLINT SMALLINT
SQL_INTEGER INTEGER
SQL_REAL REAL
SQL_FLOAT FLOAT(p)
SQL_DOUBLE DOUBLE PRECISION
SQL_BIT BIT
TINYINT TINYINT
SQL_BIGINT BIGINT
SQL_BINARY BINARY(n)
SQL_VARBINARY VARBINARY(n)
SQL_LONGVARBINARY LONG VARBINARY
SQL_TYPE_DATE DATE
SQL_TYPE_TIME TIME(p)

Share ThisShare This

Informit Network