MySQL Forums
Forum List  »  Connector/C++

I'm using c++ with prepared db
Posted by: Kale Laxman
Date: November 22, 2024 01:22AM

#include <iostream> #include <windows.h> #include <sql.h> #include <sqlext.h> #include <string> #include <cstring> #include"stdafx.h" // Constants #define MAX_NAME_SIZE 256 #define OSUCCESS 0 #define OFAILURE -1 // Global variables SQLHENV m_SqlEnv = NULL; // ODBC environment handle SQLHDBC m_SqlConn = NULL; // ODBC connection handle SQLHSTMT m_SqlStmt = NULL; // ODBC statement handle bool m_bIsOpen = false; // Connection status flag // Function prototypes void get_Error(SQLSMALLINT handleType, SQLHANDLE handle); bool DFT_ODBC_Initialize(); void Cleanup(); int _tmain(int argc, _TCHAR* argv[]) {   // Static initialization flag   static int iIsInitialized = 0;   // Connection string construction   char connstr[500] = "DSN=";   const char* p_cpDSNName = "mysql_dsn";   const char* p_cpUserName = "root";   const char* p_cpPassword = "dedupe";      SQLHANDLE m_SqlStmt;   SQLHANDLE m_SqlConn;   //  static int iIsInitialized = 0;   char        m_cUserName[MAX_NAME_SIZE]; // create member char array varaible   char        m_cPassword[MAX_NAME_SIZE]; // create member char array varaible   char        m_cDSNName[MAX_NAME_SIZE];// create member char array varaible   strcat(connstr, p_cpDSNName);   /*strcat(connstr,";Database=");   strcat(connstr,p_cpDBName);*/   strcat(connstr, ";UID=");   strcat(connstr, p_cpUserName);   strcat(connstr, ";PWD=");   strcat(connstr, p_cpPassword);   strcat(connstr, ";MARS_Connection=Yes");   if (iIsInitialized == 0) // This is to ensure that , it will be called once in the lifetime of the program.   {     if (!DFT_ODBC_Initialize())     {       MessageBox(NULL, "FAILED", "Initialization of ODBC failed.", MB_OK);       return OFAILURE;     }     else       iIsInitialized = 1;   }   //m_bIsOpen = false; // set m_bIsOpen to false   try   {     memset(m_cUserName, 0, MAX_NAME_SIZE); // set memory of size m_cUserName     memset(m_cPassword, 0, MAX_NAME_SIZE);// set memory of size m_cPassword     memset(m_cDSNName, 0, MAX_NAME_SIZE);// set memory of size m_cServiceName     strcpy(m_cUserName, p_cpUserName);//string copy p_cpUserName to m_cUserName     strcpy(m_cPassword, p_cpPassword);//string copy p_cpPassword to m_cPassword     strcpy(m_cDSNName, p_cpDSNName);//string copy p_cpServiceName to m_cServiceName     if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_DBC, m_SqlEnv, &m_SqlConn)))     {       get_Error(SQL_HANDLE_ENV, m_SqlEnv);     }     SQLCHAR retconn[1024];     //if(!SQL_SUCCEEDED(SQLConnect (m_SqlConn ,(SQLCHAR *) m_cDSNName, SQL_NTS, (SQLCHAR *)m_cUserName, SQL_NTS,(SQLCHAR *) m_cPassword, SQL_NTS)))     if (!SQL_SUCCEEDED(SQLDriverConnect(m_SqlConn, NULL, (SQLCHAR*)connstr, SQL_NTS, retconn, 1024, NULL, SQL_DRIVER_NOPROMPT)))     {       get_Error(SQL_HANDLE_DBC, m_SqlConn);     }     //MessageBox(NULL,(char*)retconn,"connstr",MB_OK);     if (!SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, m_SqlConn, &m_SqlStmt)))     {       get_Error(SQL_HANDLE_STMT, m_SqlStmt);     }     if (!SQL_SUCCEEDED(SQLSetStmtAttr(m_SqlStmt, SQL_ATTR_QUERY_TIMEOUT, (SQLPOINTER)3600, SQL_IS_INTEGER)))     {       get_Error(SQL_HANDLE_STMT, m_SqlStmt);     }     if (!SQL_SUCCEEDED(SQLSetConnectAttr(m_SqlConn, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER)3600, 0)))     {       get_Error(SQL_HANDLE_DBC, m_SqlConn);     }     if (!SQL_SUCCEEDED(SQLSetConnectAttr(m_SqlConn, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_ON, 0)))     {       get_Error(SQL_HANDLE_DBC, m_SqlConn);     }     else     {       // Prepare SQL statement with parameter placeholder       const char* query = "SELECT * FROM base WHERE uuid = ?";       if (!SQL_SUCCEEDED(SQLPrepare(m_SqlStmt, (SQLCHAR*)query, SQL_NTS))) {         get_Error(SQL_HANDLE_STMT, m_SqlStmt);         return OFAILURE;       }       // Create a buffer to hold the UUID value for binding       SQLCHAR uuidParam[37]; // 36 characters for UUID + null terminator       // Bind the parameter once       if (!SQL_SUCCEEDED(SQLBindParameter(m_SqlStmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 37, 0, &uuidParam, 0, NULL))) {         get_Error(SQL_HANDLE_STMT, m_SqlStmt);         return OFAILURE;       }       // Generate and use 1000 UUIDs (For demonstration, using uuid-1, uuid-2, ..., uuid-1000)       for (int i = 1; i <= 10000; ++i) {         // Create a simple UUID string: "uuid-1", "uuid-2", ..., "uuid-1000"         snprintf((char*)uuidParam, sizeof(uuidParam), "%d", i);         // Execute the prepared statement         if (!SQL_SUCCEEDED(SQLExecute(m_SqlStmt))) {           get_Error(SQL_HANDLE_STMT, m_SqlStmt);           continue; // Skip to the next UUID if execution fails         }         // Fetch and display the results for the current UUID         SQLINTEGER id;         SQLCHAR column_name[100];         SQLCHAR column_value[100];         // Iterate over the rows for the current UUID         while (SQL_SUCCEEDED(SQLFetch(m_SqlStmt))) {           // Example of fetching columns (change based on your table schema)           SQLGetData(m_SqlStmt, 1, SQL_C_SLONG, &id, 0, NULL); // Assuming 1st column is an integer (ID)           SQLGetData(m_SqlStmt, 2, SQL_C_CHAR, column_name, sizeof(column_name), NULL); // 2nd column: name           SQLGetData(m_SqlStmt, 3, SQL_C_CHAR, column_value, sizeof(column_value), NULL); // 3rd column: value           // Print the result for this row           std::cout << "UUID: uuid-" << i << ", ID: " << id << ", Name: " << column_name << ", Value: " << column_value << std::endl;         }         // Reset the statement for the next UUID (optional)         SQLFreeStmt(m_SqlStmt, SQL_CLOSE); // Close the result set to reuse the statement handle for the next UUID       }       // Cleanup       SQLFreeHandle(SQL_HANDLE_STMT, m_SqlStmt);       SQLFreeHandle(SQL_HANDLE_DBC, m_SqlConn);       return OSUCCESS;     }   }   catch (...)   {     return OFAILURE;   }   return true;//m_bIsOpen; // return the m_bIsOpen value } // Error handling function to retrieve and display ODBC error details void get_Error(SQLSMALLINT handleType, SQLHANDLE handle) {   SQLCHAR SQLState[6], message[256];   SQLINTEGER nativeError;   SQLSMALLINT msgLen;   // Retrieve error message   SQLGetDiagRec(handleType, handle, 1, SQLState, &nativeError, message, sizeof(message), &msgLen);   // Display error message   std::cerr << "ODBC Error: " << message << " (SQLState: " << SQLState << ", NativeError: " << nativeError << ")" << std::endl; } // Initialize the ODBC environment bool DFT_ODBC_Initialize() {   if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_SqlEnv) != SQL_SUCCESS) {     std::cerr << "Unable to allocate ODBC environment handle." << std::endl;     return false;   }   // Set the ODBC version to 3.x   if (SQLSetEnvAttr(m_SqlEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0) != SQL_SUCCESS) {     std::cerr << "Unable to set ODBC version." << std::endl;     SQLFreeHandle(SQL_HANDLE_ENV, m_SqlEnv);     return false;   }   return true; }



In this above code at line Reset the statment for next uuid option .. it's not able free the memory.. at every iteration memory is increasing, can help with this , for this memory issue

Options: ReplyQuote


Subject
Views
Written By
Posted
I'm using c++ with prepared db
519
November 22, 2024 01:22AM
260
November 22, 2024 06:59AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.