PoomrokC's blog

By PoomrokC, history, 8 years ago, In English

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

  • Vote: I like it
  • -15
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by PoomrokC (previous revision, new revision, compare).

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Last three days are like nightmares to me , I try to install Mysql Connector/C++ on my Ubuntu14.04 server

that's because you're doing it wrong. Nobody writes compile commands by hand, there's cmake for such things

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I already used Cmake. You should try it sometimes , follow the official documentation and all the errors come up all the time. The official tutorial also never mention that we can get it through apt-get, that's why I wrote this guide. You are already so smart , you can just skip this guide :)

»
8 years ago, # |
  Vote: I like it +11 Vote: I do not like it

Well article seems to be good and interesting, but I'm not sure if CF is right place for it. You might consider starting your own blog for such things, since CF is mostly for competitive-programming related stuff.

  • »
    »
    8 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    BINGO!! Nice reason for downvotes. I tried to find rules about writing blog and no rule that said it should only be about cp. I mean , it's a coding community right? Some people may find it useful,others don't. But okay,no more blogs :) . HOPE YOU GUYS HAPPILY LIVE IN THE NICE CP WORLD AND KEEP DOWNVOTING :)

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +23 Vote: I do not like it

      Please don't be offended. In my opinion, down/upvotes should work as a measure of how useful that piece of information is. Your blog, unfortunately, is not useful to most people in this community. Simply because it IS a competitive programming community.

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by PoomrokC (previous revision, new revision, compare).

»
8 years ago, # |
  Vote: I like it +20 Vote: I do not like it

on Ubuntu

Just install Gentoo.