Newbie's guide to Mysql Connector/C++ on Ubuntu Server(Mainly 14.04 , but should work on 12.04,12.10,15.04,15.10 as well )

Правка en3, от PoomrokC, 2017-04-11 21:32:30

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

Example Code

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:

Bash Script

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!!

PoomrokC

Теги c++, mysql, ubuntu

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский PoomrokC 2017-04-11 21:32:30 1
en2 Английский PoomrokC 2016-11-11 13:02:05 21 Tiny change: 'ding!!\n\npoomrokc' -
en1 Английский PoomrokC 2016-11-11 12:58:16 3595 Initial revision (published)