Hi everyone , this is my first blog entry and it is about how to connect to Mysql with C++ language.↵
#### Motivation of writing this guide↵
↵
Last three days are like nightmares to me , I try to install Mysql Connector/C++ on my Ubuntu14.04 server. Guides on Official Mysql site is somewhat very confusing and I think some part is wrong. So I'd like to write this guide to whoever finds it difficult to install it and maybe for my future use in case I mess up my own server!!↵
↵
#### 1.Installing LAMP stack if you don't already have one.↵
↵
You should already have mysql installed if you read this guide , but in case you don't , Follow a nice guide [here](https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04). You can choose to only install mysql , but somehow I feel safer to install all of them.↵
↵
#### 2.Install the Mysql Connector/C++↵
↵
As I already say , You can follow the guide on Mysql Official site but some commands on that are wrong , so I finally choose to trust apt-get,so run this in TERMINAL:↵
↵
`sudo apt-get update`↵
↵
`sudo apt-get install libmysqlcppconn-dev`↵
↵
If this complete successfully you should have the connector library installed.↵
↵
#### Compiling and testing standalone program↵
Consider the following example code which only connect to the database.↵
↵
<spoiler summary="Example Code">↵
~~~~~↵
#include <stdlib.h>↵
#include <iostream>↵
#include <sstream>↵
#include <stdexcept>↵
#include "mysql_connection.h"↵
#include <cppconn/driver.h>↵
#include <cppconn/exception.h>↵
#include <cppconn/resultset.h>↵
#include <cppconn/statement.h>↵
#include <cppconn/prepared_statement.h>↵
#define EXAMPLE_HOST "localhost"↵
#define EXAMPLE_USER "yourusername"↵
#define EXAMPLE_PASS "yourpassword"↵
#define EXAMPLE_DB "yourdatabase"↵
using namespace std;↵
int main(int argc, const char **argv)↵
{↵
const string url=EXAMPLE_HOST;↵
const string user=EXAMPLE_USER;↵
const string pass=EXAMPLE_PASS;↵
const string database=EXAMPLE_DB;↵
sql::Driver* driver = get_driver_instance();↵
std::auto_ptr<sql::Connection> con(driver->connect(url, user, pass));↵
con->setSchema(database);↵
return 0;↵
}↵
↵
↵
~~~~~↵
</spoiler>↵
↵
Just don't forget to change your host,user,pass,database Or you will get Runtime error. Save this code as tester.cpp.↵
↵
We will compile this code using ↵
↵
`sudo g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn`↵
↵
Which make executable name 'testapp'.↵
↵
For the sake of simplicity , I created this bash script to make compiling easier:↵
↵
<spoiler summary="Bash Script">↵
~~~~~↵
#!/bin/bash↵
sudo g++ -Wall -I/usr/include/cppconn -o $2 $1 -L/usr/lib -lmysqlcppconn↵
↵
~~~~~↵
</spoiler>↵
↵
and save this as cp.sh , so next time you want to compile 'tester.cpp' in to executable 'testapp' just use↵
↵
`./cp.sh tester.cpp testapp`↵
↵
Then run your very first program by running↵
↵
`./testapp`↵
↵
If your code perform successfully , nothing should be printed. If there are errors(exeption), just read and try to deal with it.↵
↵
#### Summary↵
I'm not a professional so if there are mistakes I'm so sorry for it. I just want to help people that have the same problem using C++ with Mysql. For more example about the connector usage, I'm afraid you must find it somewhere else because I'm also studying and may not be able to provide you with the correct information. ↵
↵
Thanks everybody for reading my post!↵
↵
Happy Coding!!↵
↵
[user:poomrokc,2016-11-11]
#### Motivation of writing this guide↵
↵
Last three days are like nightmares to me , I try to install Mysql Connector/C++ on my Ubuntu14.04 server. Guides on Official Mysql site is somewhat very confusing and I think some part is wrong. So I'd like to write this guide to whoever finds it difficult to install it and maybe for my future use in case I mess up my own server!!↵
↵
#### 1.Installing LAMP stack if you don't already have one.↵
↵
You should already have mysql installed if you read this guide , but in case you don't , Follow a nice guide [here](https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-14-04). You can choose to only install mysql , but somehow I feel safer to install all of them.↵
↵
#### 2.Install the Mysql Connector/C++↵
↵
As I already say , You can follow the guide on Mysql Official site but some commands on that are wrong , so I finally choose to trust apt-get,so run this in TERMINAL:↵
↵
`sudo apt-get update`↵
↵
`sudo apt-get install libmysqlcppconn-dev`↵
↵
If this complete successfully you should have the connector library installed.↵
↵
#### Compiling and testing standalone program↵
Consider the following example code which only connect to the database.↵
↵
<spoiler summary="Example Code">↵
~~~~~↵
#include <stdlib.h>↵
#include <iostream>↵
#include <sstream>↵
#include <stdexcept>↵
#include "mysql_connection.h"↵
#include <cppconn/driver.h>↵
#include <cppconn/exception.h>↵
#include <cppconn/resultset.h>↵
#include <cppconn/statement.h>↵
#include <cppconn/prepared_statement.h>↵
#define EXAMPLE_HOST "localhost"↵
#define EXAMPLE_USER "yourusername"↵
#define EXAMPLE_PASS "yourpassword"↵
#define EXAMPLE_DB "yourdatabase"↵
using namespace std;↵
int main(int argc, const char **argv)↵
{↵
const string url=EXAMPLE_HOST;↵
const string user=EXAMPLE_USER;↵
const string pass=EXAMPLE_PASS;↵
const string database=EXAMPLE_DB;↵
sql::Driver* driver = get_driver_instance();↵
std::auto_ptr<sql::Connection> con(driver->connect(url, user, pass));↵
con->setSchema(database);↵
return 0;↵
}↵
↵
↵
~~~~~↵
</spoiler>↵
↵
Just don't forget to change your host,user,pass,database Or you will get Runtime error. Save this code as tester.cpp.↵
↵
We will compile this code using ↵
↵
`sudo g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn`↵
↵
Which make executable name 'testapp'.↵
↵
For the sake of simplicity , I created this bash script to make compiling easier:↵
↵
<spoiler summary="Bash Script">↵
~~~~~↵
#!/bin/bash↵
sudo g++ -Wall -I/usr/include/cppconn -o $2 $1 -L/usr/lib -lmysqlcppconn↵
↵
~~~~~↵
</spoiler>↵
↵
and save this as cp.sh , so next time you want to compile 'tester.cpp' in to executable 'testapp' just use↵
↵
`./cp.sh tester.cpp testapp`↵
↵
Then run your very first program by running↵
↵
`./testapp`↵
↵
If your code perform successfully , nothing should be printed. If there are errors(exeption), just read and try to deal with it.↵
↵
#### Summary↵
I'm not a professional so if there are mistakes I'm so sorry for it. I just want to help people that have the same problem using C++ with Mysql. For more example about the connector usage, I'm afraid you must find it somewhere else because I'm also studying and may not be able to provide you with the correct information. ↵
↵
Thanks everybody for reading my post!↵
↵
Happy Coding!!↵
↵
[user:poomrokc,2016-11-11]