Friday, February 24, 2012

Setup connection to MySQL in VS 2010

Author: 
Source: http://arunasujith.blogspot.com/2012/02/mysql-c-connection-example-in-visual_10.html


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... :)