Retrieving Data Type Information
- Last Updated: October 7, 2020
- 2 minute read
- DataDirect Connectors
- ODBC
- Cloudera Impala 7.1
- dBase 7.1
- Flat files/Text 7.1
- MySQL 7.1
- Pervasive (Btrieve) 7.1
- XML 7.1
- Documentation
Retrieving Data Type Information
At times, you might need to get information about the data types that are supported by the data source, for example, precision and scale. You can use the ODBC function SQLGetTypeInfo to do this.
On Windows, you can use ODBC Test to call SQLGetTypeInfo against the ODBC data source to return the data type information.
Refer to Diagnostic tools in the Progress DataDirect for ODBC Drivers Reference for details about ODBC Test.
On UNIX, Linux, or Windows, an application can call SQLGetTypeInfo. Here is an example of a C function that calls SQLGetTypeInfo and retrieves the information in the form of a SQL result set.
void ODBC_GetTypeInfo(SQLHANDLE hstmt, SQLSMALLINT dataType)
{
RETCODE rc;
// There are 19 columns returned by SQLGetTypeInfo.
// This example displays the first 3.
// Check the ODBC 3.x specification for more information.
// Variables to hold the data from each column
char typeName[30];
short sqlDataType;
unsigned long columnSize;
SQLINTEGER strlenTypeName,
strlenSqlDataType,
strlenColumnSize;
rc = SQLGetTypeInfo(hstmt, dataType);
if (rc == SQL_SUCCESS) {
// Bind the columns returned by the SQLGetTypeInfo result set.
rc = SQLBindCol(hstmt, 1, SQL_C_CHAR, &typeName,
(SDWORD)sizeof(typeName), &strlenTypeName);
rc = SQLBindCol(hstmt, 2, SQL_C_SHORT, &sqlDataType,
(SDWORD)sizeof(sqlDataType), &strlenSqlDataType);
rc = SQLBindCol(hstmt, 3, SQL_C_LONG, &columnSize,
(SDWORD)sizeof(columnSize), &strlenColumnSize);
// Print column headings
printf ("TypeName DataType ColumnSize\n");
printf ("-------------------- ---------- ----------\n");
do {
// Fetch the results from executing SQLGetTypeInfo
rc = SQLFetch(hstmt);
if (rc == SQL_ERROR) {
// Procedure to retrieve errors from the SQLGetTypeInfo function
ODBC_GetDiagRec(SQL_HANDLE_STMT, hstmt);
break;
}
// Print the results
if ((rc == SQL_SUCCESS) || (rc == SQL_SUCCESS_WITH_INFO)) {
printf ("%-30s %10i %10u\n", typeName, sqlDataType, columnSize);
}
} while (rc != SQL_NO_DATA);
}
}
For information about how a database’s data types map to the standard ODBC data types, see the appropriate driver chapter in this book.