Thread: C++ connect to MySQL

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    C++ connect to MySQL

    I have a program that access MySQL.

    I use the following method to connect to MySQL.

    Code:
    void ConnectDB()
    {
      HENV hEnv = NULL;
      HDBC hDBC = NULL;
      RETCODE retcode;        
    			
      char* szUserName = (char*) sLogin.c_str();
      char* szPassword = (char*) strdbPassword.c_str();
      char* szDNS = (char*) sDSN.c_str();
            
            
      SQLAllocEnv(&hEnv);
      SQLAllocConnect (hEnv, &hDBC);
      retcode = SQLConnect (hDBC, (UCHAR*)szDNS, SQL_NTS, (UCHAR*)  szUserName, SQL_NTS, (UCHAR*)szPassword, SQL_NTS);
            
      if (retcode1 == SQL_SUCCESS || retcode1 == SQL_SUCCESS_WITH_INFO)
      {
         printf("Database connect sucessfully.\n");
      } 
      else
      {
        printf("Database connect fail.\n");
      }
    
      SQLDisconnect( hDBC );
      SQLFreeConnect( hDBC );
      SQLFreeEnv( hEnv );
    }
    
    int main()
    {
      while( 1 )
      {
        ConnectDB();
        Sleep(1000);
      }
    
      return 0;
    }
    When I run this program, I monitor the memory usage through Task Manager,
    I found that the memory usage will increase.

    I have done some reserch about the ODBC connection method, some claim that this method will cause memory leak some how.

    Can anyone help me on this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Can anyone help me on this?
    Maybe post it on the MySQL forum where there are likely to be a high concentration of people with vastly more experience of MySQL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to connect mysql in mfc.
    By sgh in forum C++ Programming
    Replies: 1
    Last Post: 12-28-2008, 12:41 AM
  2. MySQL libraries
    By csonx_p in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2008, 02:23 AM
  3. Connect to server and get info from mysql db.
    By Sam Granger in forum C++ Programming
    Replies: 2
    Last Post: 01-26-2006, 06:11 AM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  5. MySQL Connect from Outside Computer
    By juschillin in forum Windows Programming
    Replies: 0
    Last Post: 09-27-2002, 08:02 AM