Pages

Subscribe:

Ads 468x60px

Friday, February 10, 2012

MySQL C++ Connection Example in Visual Studio

Creating a MySQL database connection using C++ is  really easy. But for a beginner it can be really hard to find the correct procedure cause it will gave really troubles. At the first time when I'm doing it it took me quite some time to make it work. So I'm going to explain how to make a connection to the MySQL database using C++ code. We use the library called boost to make the connection. 

Step 1 (Download the boost library)
First step is to download the boost library. You can download it from here from the official download page Or you can download the one I've compressed and uploaded which is smaller in size from here.

Step 2 (Create the project using Visual Studio 2008)
First go  to File --> New --> Project and you'll get a screen like this. 

mysql c++ connection example

Select Visual C++ --> Win 32 --> Win 32 Console Application  and give a name to the project and press OK. 
Then set Application Type to Console Application and remember to tick the empty project. and click finish.

Then Right Click the project and add new Item a cpp source file called Connect.cpp.

// Standard includes
#include 
#include 
#include 
using namespace std;

// Connector Includes

#include "cppconnection/driver.h"
#include "cppconnection/exception.h"
#include "cppconnection/resultset.h"
#include "cppconnection/statement.h"


// Link to the Connector/C++ library
#pragma comment(lib, "mysqlcppconn.lib")

// Connection details
string server   = "localhost";
string username = "root";
string password = "123"; // 

int main(){
    sql::Driver     *driver; // MySQL Driver Object
    sql::Connection *connection; // MySQL connection Object
    sql::Statement  *statement;   // Statement which holds SQL commands
    sql::ResultSet  *resultSet;    // ResultSet to hold the results
    //Here is the connection
    try{
        driver = get_driver_instance();
        connection = driver->connect(server, username, password);
        statement = connection->createStatement();
        statement->execute("USE performance_schema");
        resultSet = statement->executeQuery("show tables");
        while (resultSet->next()){
            // Iterating the result set
            cout << resultSet->getString(1) << endl;                
        }
    }
    catch (sql::Exception e){
        cout << "Error message: " << e.what() << endl;
        system("pause");
        exit(1);
    }

    //Clear resources
    delete res;
    delete statement;
    delete connection;

    system("pause");
    return 0;
}

Copy the boost folder into the project folder. In this example the folder is Visual Studio 2008\Projects\MySQLConnection\MySQLConnection folder.

Then download this header and dll files and extract them into the same folder Visual Studio 2008\Projects\MySQLConnection\MySQLConnection folder.

Then the final step is right cilck on the Project and go to 
Properties --> Configuration Properties --> C/C++ --> General . 
On the right hand side set the "Additional Include Directories" value to ".\" value. (with out the quotes)

That's it build your project and run the project. And one more final thing when you run the project set the  "Solution Configuration" to Release mode. In the Debug Mode I had some error which I still don't no why.

So that's it now I think you got a clear idea about creating a connection for MySQL using C++ in Visual Studio 2008. See you around with another exciting post... :)

17 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thanx a lot for you post I have fix it I manually copy libmysq.dll,mysqlcppconn.dll and mysqlcppconn.lib to my project folder. Then it works.

    Really thnx for the article.

    ReplyDelete
  5. Please tell me is there a way I can stop getting the cmd open after performing a database transaction.
    After it saves data to the data base it gets open a cmd and say please enter any key to continue... after entering a key it will close the application.
    Please let me know.
    This is the code
    //Here is the connection
    try{
    driver = get_driver_instance();
    connection = driver->connect(server, username, password);
    statement = connection->createStatement();

    resultSet = statement->executeQuery("INSERT student.student values('a');
    cout << "Error message: ";

    }
    catch (exception e){
    cout << "Error message: " << e.what() << endl;
    system("pause");
    exit(1);
    }

    ReplyDelete
  6. Did you create a console application??.. Try removing the

    system("pause");

    line

    ReplyDelete
  7. Replies
    1. This comment has been removed by the author.

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Is it necessary to use Boost cant we connect it using C++ connector?

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Hi,
    i have followed all the steps as above mentioned but i am getting error
    as foolows
    1)Error C1083 Cannot open include file: 'boost/variant.hpp': No such file or directory

    please help me sir i have been trying this from long days,anybodys help is apprecieted
    thanks

    ReplyDelete
  12. Hi,
    i have followed all the steps as above mentioned but i am getting error
    as foolows
    1)Error C1083 Cannot open include file: 'boost/variant.hpp': No such file or directory

    please help me sir i have been trying this from long days,anybodys help is apprecieted
    thanks

    ReplyDelete
  13. Thanks for your code. However, I am getting the error, "can't connect to mysql server on 'localhost' (10061)".
    Can you offer some guidance please.
    Kind Regards
    Simon

    ReplyDelete
  14. Please disregard my question of: February 27, 2017 at 11:40 PM.

    It has been solved.

    Kind Regards

    ReplyDelete