#include #include #include #include #include #ifndef TRUE #define TRUE 1 #endif #define MAXCOLS 100 #define MAX_DATA_LEN 255 SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; 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) { std::cout << "NO DATA .. .. .." << std::endl; 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) { std::cout << " Error: " << eMsg << std::endl; } return (SQL_ERROR); } } int main(int argc, char * argv[]) { SQLRETURN rc; if (argc != 3) { std::cout << "USAGE: DBClassTet " << std::endl; return -1; } rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_ENV, henv, rc)); rc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_ENV, henv, rc)); rc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_ENV, henv, rc)); std::cout << "Connecting to " << argv[1] << std::endl; rc = SQLConnect(hdbc, (SQLCHAR *)argv[1], SQL_NTS, (SQLCHAR *)argv[2], SQL_NTS, (SQLCHAR *)argv[3], SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_DBC, hdbc, rc)); rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_DBC, hdbc, rc)); /*************************************************************************************/ SQLDOUBLE data; SQLINTEGER status; rc = SQLExecDirect(hstmt, (SQLCHAR *)"USE TEST", SQL_NTS); rc = SQLExecDirect(hstmt, (SQLCHAR *)"DROP TABLE IF EXISTS `T_LENGTH`", SQL_NTS); rc = SQLExecDirect(hstmt, (SQLCHAR *)"CREATE TABLE `T_LENGTH` (`F11` varchar(10) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1", SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); rc = SQLExecDirect(hstmt, (SQLCHAR *)"SELECT * FROM test.T_LENGTH", SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); SQLINTEGER rs; SQLSMALLINT sLength; SQLSMALLINT sType; char pColName[255]; unsigned long lLength; short sScale; short uFlag; rc = SQLDescribeCol(hstmt, 1, (SQLCHAR*)pColName,255,&sLength, &sType, &lLength,&sScale,&uFlag ); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); std::cout << "COL LENGTH -> " << lLength << std::endl; /*************************************************************************************/ rc = SQLFreeHandle(SQL_HANDLE_STMT, hstmt); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); rc = SQLDisconnect(hdbc); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_DBC, hdbc, rc)); rc = SQLFreeHandle(SQL_HANDLE_DBC, hdbc); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_DBC, hdbc, rc)); rc = SQLFreeHandle(SQL_HANDLE_ENV, henv); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_ENV, henv, rc)); return(0); }