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/en/connector-cpp-examples-complete-example-1.html

#include <stdlib.h>
#include <iostream>
#include <winsock2.h>
/*
  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;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;

try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;
cout << "Running 'SELECT 'Hello World!...1' AS _message'..." << endl;
  /* Create a connection */
  driver = get_driver_instance();
  cout << "Running 'SELECT 'Hello World!...2' AS _message'..." << endl;
  con = driver->connect("tcp://127.0.0.1:3306", "root", "");
  cout << "Running 'SELECT 'Hello World!...3' AS _message'..." << endl;
  /* Connect to the MySQL test database */
  con->setSchema("bill");
cout << "Running 'SELECT 'Hello World!...1' AS _message'..." << endl;
  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
  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;
  }
  delete res;
  delete stmt;
  delete con;

} 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;
}

cout << endl;
return EXIT_SUCCESS;
}

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 this line is raising problem
con = driver->connect("tcp://127.0.0.1:3306", "root", "");
May be require some port or network related file.

Any idea or advice will be great help for me.

Compile for debugging then learn to use your compiler's debugger to single step through the program. When you learn to do your own debugging you will be better able to find such problems yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.