#include #include #include #include #include #ifndef TRUE #define TRUE 1 #endif #define MAXCOLS 100 #define MAX_DATA_LEN 255 #define CONN_351 TEXT("DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;Regional=No;UID=root;PWD=*****;Trusted_Connection=no;DATABASE=test;Option=7") 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; 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)); TCHAR szTmp[512] = {0}; SHORT dwLen = 0; rc = SQLDriverConnect(hdbc, NULL, (SQLTCHAR *)CONN_351, _countof(CONN_351), (SQLTCHAR *)szTmp, sizeof(szTmp), &dwLen, SQL_DRIVER_NOPROMPT); 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)); /*************************************************************************************/ rc = SQLExecDirect(hstmt, (SQLCHAR *)"DROP TABLE IF EXISTS bug32270", SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); rc = SQLExecDirect(hstmt, (SQLCHAR *)"CREATE TABLE bug32270 ( Path NVARCHAR(256) )", SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); LPTSTR pData = TEXT("x"); int i = 20; rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_TCHAR, SQL_VARCHAR, 256, 0, pData, i, (SQLINTEGER*)&i); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); rc = SQLExecDirect(hstmt, (SQLCHAR *)TEXT("SELECT DISTINCT Path FROM bug32270 WHERE Upper(Path) = Upper(?)"), SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) return(PrintError(SQL_HANDLE_STMT, hstmt, rc)); std::cout << "SELECT: RetCode = " << rc << 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); }