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.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWhat is the error you are getting??..
ReplyDeleteThanx 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.
ReplyDeleteReally thnx for the article.
Please tell me is there a way I can stop getting the cmd open after performing a database transaction.
ReplyDeleteAfter 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);
}
Did you create a console application??.. Try removing the
ReplyDeletesystem("pause");
line
Ok thank u
ReplyDeleteThis comment has been removed by the author.
DeleteThis comment has been removed by the author.
ReplyDeleteIs it necessary to use Boost cant we connect it using C++ connector?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYes it is. There are boost includes.
ReplyDeleteHi,
ReplyDeletei 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
Hi,
ReplyDeletei 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
Thanks for your code. However, I am getting the error, "can't connect to mysql server on 'localhost' (10061)".
ReplyDeleteCan you offer some guidance please.
Kind Regards
Simon
Please disregard my question of: February 27, 2017 at 11:40 PM.
ReplyDeleteIt has been solved.
Kind Regards