#include #include #include #include #define CHECK_ODBC(HND, HNDTYPE, RC) \ assert(!RC) void print_result(SQLHANDLE stmt) { SQLCHAR val[50]; SQLSMALLINT c; SQLSMALLINT cols; SQLRETURN rc; SQLINTEGER rows; SQLINTEGER len; int r = 0; CHECK_ODBC(stmt, SQL_HANDLE_STMT, SQLNumResultCols(stmt, &cols)); CHECK_ODBC(stmt, SQL_HANDLE_STMT, SQLRowCount(stmt, &rows)); printf("Rows: %d\n", rows); while((rc = SQLFetch(stmt)) == SQL_SUCCESS) { printf("=== Row %d ===\n", r++); for(c = 0; c < cols; ++c) { CHECK_ODBC(stmt, SQL_HANDLE_STMT, SQLGetData(stmt, c + 1, SQL_C_CHAR, val, 50, &len)); printf("Col %d: %s\n", c + 1, len == SQL_NULL_DATA ? "" : val); } } assert(rc == SQL_NO_DATA_FOUND); } void odbc3() { SQLHANDLE env; SQLHANDLE dbc; SQLHANDLE stmt; printf("============== ODBC v3 ==============\n"); CHECK_ODBC(env, SQL_HANDLE_ENV, SQLAllocHandle(SQL_HANDLE_ENV, NULL, &env)); CHECK_ODBC(env, SQL_HANDLE_ENV, SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)); CHECK_ODBC(env, SQL_HANDLE_ENV, SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLDriverConnect(dbc, NULL, "DSN=myodbc5", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt)); CHECK_ODBC(stmt, SQL_HANDLE_STMT, SQLGetTypeInfo(stmt, SQL_TYPE_TIMESTAMP)); print_result(stmt); CHECK_ODBC(stmt, SQL_HANDLE_DBC, SQLFreeHandle(SQL_HANDLE_STMT, stmt)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLDisconnect(dbc)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLFreeHandle(SQL_HANDLE_DBC, dbc)); CHECK_ODBC(env, SQL_HANDLE_ENV, SQLFreeHandle(SQL_HANDLE_ENV, env)); } void odbc2() { SQLHANDLE env; SQLHANDLE dbc; SQLHANDLE stmt; printf("============== ODBC v2 ==============\n"); CHECK_ODBC(env, SQL_HANDLE_ENV, SQLAllocEnv(&env)); CHECK_ODBC(env, SQL_HANDLE_ENV, SQLAllocConnect(env, &dbc)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLDriverConnect(dbc, NULL, "DSN=myodbc5", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLAllocStmt(dbc, &stmt)); CHECK_ODBC(stmt, SQL_HANDLE_STMT, SQLGetTypeInfo(stmt, SQL_TIMESTAMP)); print_result(stmt); CHECK_ODBC(stmt, SQL_HANDLE_DBC, SQLFreeStmt(stmt, SQL_DROP)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLDisconnect(dbc)); CHECK_ODBC(dbc, SQL_HANDLE_DBC, SQLFreeConnect(dbc)); CHECK_ODBC(env, SQL_HANDLE_ENV, SQLFreeEnv(env)); } int main(int argc, char **argv) { odbc2(); odbc3(); }