#ifdef _WIN32 #include #endif #include #include #include #include SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; #define ok_something(A, B, C) rc= B; if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO){ \ error_num= PrintError(C, A, rc); \ } #define ok_env(A, B) ok_something(A,B, SQL_HANDLE_ENV) #define ok_dbc(A, B) ok_something(A,B, SQL_HANDLE_DBC) #define ok_stmt(A, B) ok_something(A,B, SQL_HANDLE_STMT) SQLINTEGER PrintError(SQLSMALLINT handleType, SQLHANDLE handle, SQLRETURN rcode) { SQLRETURN rc = SQL_ERROR; SQLCHAR sqlState[6]; SQLCHAR eMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER nError; SQLSMALLINT msgLen; if (rcode == SQL_NO_DATA) { printf( "NO DATA .. .. ..\n"); return(SQL_NO_DATA); } else { rc = SQLGetDiagRec(handleType, handle, 1, sqlState, &nError, eMsg, sizeof(eMsg), &msgLen); if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) { printf(" Error: %s\n", eMsg); } return (SQL_ERROR); } } int main(int argc, char * argv[]) { SQLRETURN rc; SQLCHAR connOut[255], szName[128]; /* buffer for connection output */ SQLSMALLINT szConnOut, colcount; /* num bytes returned in connOut */ SQLCHAR data[255]= {0}; SQLINTEGER cbName; SQLSMALLINT len; SQLLEN res; int error_num= 0; int p1= 3, p2= 5; printf("SIZEOF(SQLLEN) %ld\n", sizeof(SQLLEN)); if(argc < 2) { printf("\nUsage:\ntest55024 conn_string\n" "EXAMPLE:\ntest55024 \"DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;USER=myuser;PASSWORD=mypass;DATABASE=mydb;\""); return 0; } ok_env(henv, SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv)); ok_env(henv, SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_NTS)); ok_env(henv, SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc)); ok_dbc(hdbc, SQLDriverConnect(hdbc, NULL, argv[1], SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT)); ok_dbc(hdbc, SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt)); ok_stmt(hstmt, SQLExecDirect(hstmt, "DROP TABLE IF EXISTS t_test55024", SQL_NTS)); ok_stmt(hstmt, SQLExecDirect(hstmt, "CREATE TABLE t_test55024(col01 TINYINT);", SQL_NTS)); ok_stmt(hstmt, SQLExecDirect(hstmt, "SELECT * FROM t_test55024", SQL_NTS)); ok_stmt(hstmt, SQLColAttribute(hstmt, 1, SQL_DESC_TYPE, NULL, 0, &len, &res)); printf("SIZEOF(SQLLEN) %ld\n", sizeof(SQLLEN)); printf("TYPE %ld [should be: %ld]\n", res, (SQLLEN)SQL_TINYINT); ok_stmt(hstmt, SQLFreeStmt(hstmt, SQL_CLOSE)); ok_dbc(hdbc, SQLDisconnect(hdbc)); ok_dbc(hdbc, SQLFreeHandle(SQL_HANDLE_DBC, hdbc)); ok_env(henv, SQLFreeHandle(SQL_HANDLE_ENV, henv)); }