/* test case */ #include #include #include #include #include #include void CheckRC(SQLRETURN rc, char* msg, char* filename, int lineno); #define CHECKRC(FUNCSHN,MSG) CheckRC(FUNCSHN, MSG, __FILE__, __LINE__) SQLHENV hEnv = SQL_NULL_HENV; SQLHDBC hDbc = SQL_NULL_HDBC; SQLHSTMT hStmt = SQL_NULL_HSTMT; SQLCHAR* connString; SQLCHAR connOut[255]; /* buffer for connection output */ SQLSMALLINT szConnOut; /* num bytes returned in connOut */ SQLINTEGER Version = SQL_OV_ODBC3; SQLUINTEGER cursor_type = SQL_CURSOR_DYNAMIC; //SQLUINTEGER cursor_type = SQL_CURSOR_KEYSET_DRIVEN; void doTest() { char value[60001]; char stmt[100000]; int i; SQLRETURN result; SQLINTEGER length; SQLUINTEGER row_count_ptr; SQLUSMALLINT row_status_array[14]; printf("Test....\n"); CHECKRC( SQLAllocStmt(hDbc, &hStmt), "SQLAllocStmt()" ); CHECKRC( SQLSetStmtAttr(hStmt,SQL_ATTR_CURSOR_TYPE, cursor_type, 0), "SQLSetStmtAttr()"); CHECKRC( SQLExecDirect(hStmt, (SQLCHAR*)"SELECT * FROM user", SQL_NTS), "SQLExecDirect()" ); CHECKRC( result = SQLExtendedFetch(hStmt,SQL_FETCH_NEXT,1,&row_count_ptr,&row_status_array),"SQLExtendedFetch()"); if (result == SQL_NO_DATA) printf("ok\n"); CHECKRC( SQLFreeStmt(hStmt, SQL_DROP), "SQLFreeStmt()" ); } int main(int argc, char** argv) { int nPrompt = SQL_DRIVER_NOPROMPT; if (argc < 2 || strlen(argv[1]) == 0) { connString = (SQLCHAR *)"DRIVER=MySQL ODBC 3.51 Driver;UID=root;Password=******;database=mysql"; } else connString = (SQLCHAR *)argv[1]; if (SQLAllocEnv(&hEnv) != SQL_SUCCESS) { fprintf(stderr, "Unable to allocate SQLHENV\n"); return(1); } printf("Connect....\n"); SQLAllocEnv(&hEnv); CHECKRC( SQLSetEnvAttr(hEnv,SQL_ATTR_ODBC_VERSION,Version,0 ), "SQLSetEnvAttr()" ); CHECKRC( SQLAllocConnect(hEnv, &hDbc), "SQLAllocConnect()" ); CHECKRC( SQLDriverConnect(hDbc, (HWND)0, connString, SQL_NTS, connOut, 255, &szConnOut, nPrompt), "SQLDriverConnect()" ); doTest(); printf("Disconnect....\n"); CHECKRC( SQLDisconnect(hDbc), "SQLDisconnect()" ); CHECKRC( SQLFreeConnect(hDbc), "SQLFreeConnect()" ); CHECKRC( SQLFreeEnv(hEnv), "SQLFreeEnv()" ); return 0; } void CheckRC(SQLRETURN rc, char* msg, char* filename, int lineno) { #define MSG_LNG 512 SQLCHAR szSqlState[MSG_LNG]; SQLINTEGER pfNativeError; SQLCHAR szErrorMsg[MSG_LNG]; SQLSMALLINT pcbErrorMsg; SQLRETURN ret = SQL_SUCCESS; int error = 0; if (rc != SQL_SUCCESS && rc != SQL_NO_DATA_FOUND) { if (rc != SQL_SUCCESS_WITH_INFO) { fprintf(stderr, "\nERROR (%s:%d): %s\n", filename, lineno, msg); error = 1; } while (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { ret = SQLError(hEnv, hDbc, hStmt, szSqlState, &pfNativeError, szErrorMsg, MSG_LNG, &pcbErrorMsg); switch (ret) { case SQL_SUCCESS: if (rc == SQL_SUCCESS_WITH_INFO) { fprintf(stderr, "\nINFO: (%s:%d): %s\n", filename, lineno, msg); } fprintf(stderr, "*** %s\n*** ODBC Code: %s, " "Driver Code: %ld\n\n", szErrorMsg, szSqlState, pfNativeError); break; case SQL_NO_DATA_FOUND: break; default: fprintf(stderr, "*** SQLError() failed with code=%d\n\n", ret); break; } } } if (error) exit(1); }