CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Question If someone can tell me why my program crash

    Hello
    I am trying to connect MySQL database from C++ code given below-
    My development environment is -
    Window 7
    CODE::BLOCKS 10.05 with minGW mingw32-g++.exe
    MySQL Connector C++ 1.0.5\lib\debug\mysqlcppconn.lib
    Up to compilation and linking every thing is OK I followed the tutorial http://dev.mysql.com/doc/refman/5.1/...example-1.html
    Code:
    1.#include <stdlib.h>
    2.#include <iostream>
    3.#include <winsock2.h>
    4./*
    5.  Include directly the different
    6.  headers from cppconn/ and mysql_driver.h + mysql_util.h
    7.  (and mysql_connection.h). This will reduce your build time!
    8.*/
    9.#include "mysql_connection.h"
    10.
    11.#include <cppconn/driver.h>
    12.#include <cppconn/exception.h>
    13.#include <cppconn/resultset.h>
    14.#include <cppconn/statement.h>
    15.
    16.using namespace std;
    17.int main(void)18.
    {
    19.cout << endl;
    20.cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
    21.
    22.try {
    23.  sql::Driver *driver;
    24.  sql::Connection *con;
    25.  sql::Statement *stmt;
    26.  sql::ResultSet *res;
    27.cout << "Running 'SELECT 'Hello World!...1' AS _message'..." << endl;
    28.  /* Create a connection */
    29.  driver = get_driver_instance();
    30.  cout << "Running 'SELECT 'Hello World!...2' AS _message'..." << endl;
    31.  con = driver->connect("tcp://127.0.0.1:3306", "root", "");
    32.  cout << "Running 'SELECT 'Hello World!...3' AS _message'..." << endl;
    33.  /* Connect to the MySQL test database */
    34.  con->setSchema("bill");
    35.cout << "Running 'SELECT 'Hello World!...1' AS _message'..." << endl;
    36.  stmt = con->createStatement();
    37.  res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
    38.  while (res->next()) {
    39.    cout << "\t... MySQL replies: ";
    40.    /* Access column data by alias or column name */
    41.    cout << res->getString("_message") << endl;
    42.    cout << "\t... MySQL says it again: ";
    43.    /* Access column fata by numeric offset, 1 is the first column */
    44.    cout << res->getString(1) << endl;
    45.  }
    46.  delete res;
    47.  delete stmt;
    48.  delete con;
    49.
    50.} catch (sql::SQLException &e) {
    51.  cout << "# ERR: SQLException in " << __FILE__;
    52.  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
    53.  cout << "# ERR: " << e.what();
    54.  cout << " (MySQL error code: " << e.getErrorCode();
    55.  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    56.}
    57.
    58.cout << endl;
    59.return EXIT_SUCCESS;
    60.}
    When I run the code it shows
    SELECT 'Hello World!
    SELECT 'Hello World!....1
    SELECT 'Hello World!....2
    And then Debug window open with a message "bill.exe has stopped working", I copied libmysql.dll to my application debug folder.
    My assumption is that line no. 31 is raising problem because after that cout for Hello World!...3 is not executing.

    May be require some port or network related file.
    Any idea or advice will be great help for me.
    Thank you

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: If someone can tell me why my program crash

    Did you use the debugger to debug your code?
    What if either con, stmt or res are nullptrs? The example relies on them not being nullptrs, but if they are, your program will crash as it does.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: If someone can tell me why my program crash

    Quote Originally Posted by vishalon View Post
    Hello
    I am trying to connect MySQL database from C++ code given below-
    My development environment is -
    Window 7
    CODE::BLOCKS 10.05 with minGW mingw32-g++.exe
    MySQL Connector C++ 1.0.5\lib\debug\mysqlcppconn.lib
    Up to compilation and linking every thing is OK I followed the tutorial http://dev.mysql.com/doc/refman/5.1/...example-1.html
    Honestly, that is a poorly coded example. There is no check for errors anywhere, no check for NULL, nothing. Then you have this:
    Code:
    delete res;
    delete stmt;
    delete con;
    Are you sure that the memory here was allocated with new? Even if it is, your code has a memory leak if it throws an exception after any of those items are allocated. The catch block doesn't remove any allocated resources. So the code has all sorts of problems if anything happens to fail.

    I have never used this particular MySQL library, but here is a much cleaner implementation of that example program:
    Code:
    /* Standard C++ includes */
    #include <stdlib.h>
    #include <iostream>
    
    /*
      Include directly the different
      headers from cppconn/ and mysql_driver.h + mysql_util.h
      (and mysql_connection.h). This will reduce your build time!
    */
    #include "mysql_connection.h"
    #include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>
    
    using namespace std;
    
    template <typename Res>
    struct SQLResource
    {
       Res* m_pRes;
       SQLResource(Res *p) : m_pRes(p) {}
      ~SQLResource() { delete m_pRes; }
    };
    
    int main()
    {
        cout << endl;
        cout << "Running 'SELECT 'Hello World!'   AS _message'..." << endl;
        try {
              sql::Driver *driver;
              sql::Connection *con;
              sql::Statement *stmt;
              sql::ResultSet *res;
    
              /* Create a connection */
              driver = get_driver_instance();
              SQLResource<sql::Driver> r1(driver);
              if ( !driver )
                 return -1;
              con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
              SQLResource<sql::Connection> r2(con);
              if ( !con )
                return -2;
             con->setSchema("test");
             stmt = con->createStatement();
             SQLResource<sql::Statement> r3(stmt);
             if ( !stmt )
                return -3;
             res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
             SQLResource<sql::ResultSet> r4(res);
             if ( !res )
                return -4;
             while (res->next()) 
             {
                   cout << "\t... MySQL replies: ";
                   /* Access column data by alias or column name */
                   cout << res->getString("_message") << endl;
                   cout << "\t... MySQL says it again: ";
                  /* Access column fata by numeric offset, 1 is the first column */
                  cout << res->getString(1) << endl;
              }
        } 
        catch (sql::SQLException &e) 
        {
          cout << "# ERR: SQLException in " << __FILE__;
          cout << "(" << __FUNCTION__ << ") on line " ยป
             << __LINE__ << endl;
          cout << "# ERR: " << e.what();
          cout << " (MySQL error code: " << e.getErrorCode();
          cout << ", SQLState: " << e.getSQLState() << " )" << endl;
        }
        return EXIT_SUCCESS;
    }
    Note that there is now a check for NULL, and that the resource is cleaned up automatically, even if the catch() block is executed. The reason why you don't need "delete res", "delete stmt", etc. anymore is the usage of that tiny SQLResource() class that I defined at the beginning of the code. That code does all of this magic, since the destructor for the object cleans up the resource that was assigned to the class.

    Basically, what I wrote could have been done using a smart pointer (std::shared_ptr), but just in case you're using VC++ that doesn't have shared_ptr, the code above will suffice for now. The bottom line is that the memory leak that would have happened is now taken care of.

    I didn't compile this code, but if it compiles, then you have to debug it to make sure where things go wrong (if they go wrong).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 24th, 2012 at 08:57 AM.

  4. #4
    Join Date
    Mar 2010
    Posts
    6

    Re: If someone can tell me why my program crash

    Dear Paul McKenzie thank you for considering and reply. I am not friendly with C++, I'm learning C++
    While compiling the code,compiler is complaining
    C:\msqldb\bill\main.cpp: In constructor 'SQLResource<Res>::SQLResource(Res*)':
    C:\msqldb\bill\main.cpp:21: error: class 'SQLResource<Res>' does not have any field named 'm_pRes'
    C:\msqldb\bill\main.cpp: In destructor 'SQLResource<Res>::~SQLResource()':
    C:\msqldb\bill\main.cpp:22: error: 'm_pRes' was not declared in this scope

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: If someone can tell me why my program crash

    Quote Originally Posted by vishalon View Post
    Dear Paul McKenzie thank you for considering and reply. I am not friendly with C++, I'm learning C++
    While compiling the code,compiler is complaining
    See my edited post. Secondly, if you're just learning C++, why are you using a third-party library to learn from? You should be learning from books, tutorials, etc.

    The MySQL library assumes you know C++ already, so when you use the library, you know exactly what you're doing. Otherwise, all you will be doing is writing apps that are full of mistakes, memory leaks, etc.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 24th, 2012 at 09:01 AM.

  6. #6
    Join Date
    Mar 2010
    Posts
    6

    Re: If someone can tell me why my program crash

    Quote Originally Posted by Paul McKenzie View Post
    See my edited post. Secondly, if you're just learning C++, why are you using a third-party library to learn from? You should be learning from books, tutorials, etc.

    The MySQL library assumes you know C++ already, so when you use the library, you know exactly what you're doing. Otherwise, all you will be doing is writing apps that are full of mistakes, memory leaks, etc.

    Regards,

    Paul McKenzie
    OK Paul can you give some guidance where I can get good tutorial on the topic I am looking for that is Using MySQL database from C++

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured