How To: Use MySQL Connector/C++ to Connect to a MySQL Database in Windows

I got this working in Linux in about ten minutes, while the same thing in Windows took me closer to three hours… The difficulty I had was that I need to get this working on multiple machines which all get wiped and reset on each boot, so it had to be entirely stand-alone with everything necessary to build in the same project. And the difficulty with that is that Connector/C++ needs the boost library to compile, which I wasn’t too keen on involving. In the end I couldn’t find a way around it and just threw the entire boost library into the same directory as the project as well as the libmysql.lib/dll and mysqlcppconn.lib libraries.

So now it works. It’s a little ugly, and the project folder comes in at 62MB, but it works. Really it’s not all that bad, if you just wanted to build and deploy something you’d end up with a couple of hundred KB executable plus a few MB of dlls. I guess I could go in and strip out some parts of the boost library which aren’t being used. But to be honest it’s been a long day and I’m sick of fighting with it, so I’ll leave that as an exercise for the reader.

Reminder / Info

  • This code is tested and working on Windows XP 32-Bit.
  • The project (i.e. download, at bottom of post) is for Microsoft Visual Studio 2008 Express
  • The version of the libs/headers included are:
    • libmysql.lib from MySQL server v1.5.36 32-Bit
    • boost library v1.45.0
    • mysqlcppconn.lib/dll from Connector/C++ v1.1.0 32-Bit
  • I really, really hate having to work with Windows.

Anyhoo – here’s the junk…

*** REALLY IMPORTANT: Build the following project in Release mode if you want it to work!! No, really. The libs and dll’s that the project uses have been built in Release mode and you can’t mix n’ match. ***

Download of Visual Studio 2008 Express project including all necessary headers/libs/dlls (i.e. it’s ready to go – and 12MB):

Credits: Thanks to TidyTutorials for the guide that got me started.

30 thoughts on “How To: Use MySQL Connector/C++ to Connect to a MySQL Database in Windows”

  1. yeah windows is a right pain to do anything even if we had access to cygwin or vmware at uni it would be easier i hate how something works perfectly on linux and you get it on windows and its like argh my head hurts already

  2. hi, I am stuck at the try to query the database step. The message error is unknown exception. Can you help me to solve it? Thank you very much.

    1. Hi Chloe,

      I assume the project compiles. If you have WAMP server installed, it’s probably worth doing some or all of the following:

      – Confirm the mysql service is running from the WAMP indicator in the bottom-right system tray,

      – Confirm (via phpmyadmin) that a database called “mysql” exists. Phpmyadmin is accessible by going to http://localhost in a web browser while all WAMP server services are running,

      – Confirm that you can log into mysql using mysqladmin -u root (the mysqladmin.exe utility will be in C:\wamp\bin\mysqlx.y.z\mysql or such) so that you know it’s not a username/password issue,

      – Try turning your firewall off to make sure it’s not blocking traffic on port 3306

      Put some debug output in the code if you’re not sure where it’s failing. But if I understand you correctly, the query database block is running, but generating an exception so you see output of: “SQL error. Error message: unknown exception”, right? As long as you can use mysql.exe command line util (found in the same place as the mysqladmin on mentioned above), and the commands use mysql; followed by show tables; actually show you a list of tables in the database then we can rule out that MySQL isn’t functioning at all.

      Have a go and let me know how you go on – and remember, the more info you give me to work with the better I’m likely to be able to help you! If there are error messages, please provide the exact and complete error messages.

      Also, some further reading: Introduction to MySQL


  3. Thank you very much. =D
    It works now. May I know how to select and print out the information inside one table? Sorry for the trouble.

  4. Hi,
    Sorry to bother you again. I cant seem to add the insert data into database part into my code.

    There is always a red color line under template regardless of where I tried to put #include . Can you help me solve this? Thank you.

    Edited for brevity by r3dux – 2011-04-14

    1. That code looks awfully familiar ;)

      Ah, I see what you’re getting at – you’ve got the templated toString function all messed up. Also, the way I did it in the slides (i.e. the way you did it) isn’t a particularly nice way of creating SQL queries.

      I’ve done a better way of inserting data using prepared queries and other such stuff in another slide set, I just haven’t posted it – I’ll clean up the slides and post tomorrow & you’ll be all set.

      If you want to have a go yourself before then, the templated toString function should look like this:

      And to use it, you’ll need to include the following:

      Here’s some example code to demonstrate how the toString function works:

  5. hi,

    may i know how to link c++ window form application using button with mysql?

    like when i press the button in the form, it will link to mysql.

    so i can proceed with add/delete/update to mysql.

    thanks in advance.

    1. Hi Michael,

      Sorry, but I have no idea (I don’t really code GUIs in Windows) but this looks like it’ll show you how to trigger functions from buttons in a Windows form:

      It sounds like a pretty common thing people would want to do, so I’m sure there’s plenty of tutorials/video guides out there. Have a look around & give it a go. If you really can’t get it I’ll have a look and try to knock a simple “call-a-function-when-you-press-a-button” example together for you, but Windows programming is definitely not my forte, k?

      Best wishes,

  6. Hi, Im currently having some probs with mysql. im using vs2010 and wamp, yet i have problem whenever i debug:
    Could not connect to database. Error message: Unknown exception.

    Do you know the solution to it?

    1. Um, maybe. You cannot build the provided project in Debug mode, you have to build it in Release mode, because there are no debug symbols in the provided libraries, as mentioned in the article. If you want to build it in debug mode, you need to obtain and link to debug versions of the the libraries (libmysql, Connector/C++ etc).

      If you are building in Release mode and it’s still not working, then I’m not so sure. The connect to server catch block is triggering, i.e. this bit:

      But, I’ve just tested the code with Visual Studio 2008 and I get:

      Could not connect to database. Error message: Can’t connect to MySQL server on ‘’ (10061) – if the MySQL server isn’t running or is pointed at an bad address,
      SQL error. Error message: Access denied for user ”@’localhost’ to database ‘mysql’ – if MySQL is running and we connect to it, but provide an invalid username, and
      Could not connect to database. Error message: Access denied for user ‘root’@’localhost’ (using password: YES) – if MySQL is running and we connect but provide an invalid password.

      Again, I think it’ll be a library thing and either an incompatibility with VS2010 (in which case perhaps you’ll need to get libraries compiled for VS2010), or you’re trying to run 32-bit libs on a 64-bit OS and it’s not liking it.

      Best of luck & hope you get it sorted,

  7. I’m getting the same error as Chloe. Did you find out what was causing her problem?

    I followed your suggestions to her:
    – Confirm the mysql service is running from the WAMP indicator in the bottom-right system tray,
    – Confirm (via phpmyadmin) that a database called “mysql” exists.
    – Can log in using “msql.exe -u root”, & issue “use mysql” & “show tables”
    – Turned firewall off

    I put some debug messages and it looks like the error happens when doing:
    stmt->execute(“USE mysql”);

    Everything compiled ok under MS VC++ 2010 Express (Release build).
    I was going to try w/ 2008, but having trouble downloading it at the moment.
    Would appreciate any help.

    1. Hi James,

      It sounds like you’re doing everything right – service is running, can see & connect to database from MySQL tools etc….

      If you’re getting an unknown exception error (which I think is the error you’re referring to) then I’d have to guess that it’s got to be some kind of library mismatch – try getting a new version of Connector/C++ for Visual Studio 2010 and substitute in those dlls for any in the existing project. Also, the libs I provided with the precompiled project are 32-bit, if your system is 64-bit it’s not going to work.

      Also if you separate out each line of “risky” code into its own separate try/catch block then you’ll know exactly which statement is failing, like this:

      1. I think you’re right about needing to recompile Connector/C++ under VS 2010.

        Was able to obtain VS 2008 Express and it works fine now.
        Thanks for the help & the great sample code!

  8. It’s a bit strange for me.
    In my case, it only works with DEBUG mode.
    In RELEASE mode, it spit out message like “Could not connect to database. Error message: Unknown exception”.

    I tested with: Windows 7 SP1 Pro x64 + MySQL x64 5.5.12 + VS 2010 SP1
    (I converted the original project into VS 2011 project)

    To run it in RELEASE model, I had to replace “mysqlcppconn.dll” and “mysqlcppconn.lib”.
    (I extracted the files from “” at

    Did I do something wrong?

  9. If I try to run it again in DEBUG mode after I replace the files (to run it in RELEASE mode and it works),

    then it spit out message like “Could not connect to database. Error message: Access denied for user ‘ODBC’@’localhost’ (using password: NO)”

    I don’t know what is wrong with me.

    Any advice will be appreciated.


  10. I originally wrote this code using XP and Visual Studio 2008 and it worked correctly when built in Release mode – if you’re going to use this with Windows 7 and/or Visual Studio I’d recommend creating a new project entirely and copying and pasting the code in then setting up the libraries of the VS project, as something’s obviously going wrong with the conversion.

    I recently had to cover this material in a class I’m teaching (like, last week) and it worked fine with Windows 7 and Visual Studio 2008 – so it must be a VS2010 thing.

    This might help you out (but remember to get rid of the password if you’re using WAMP Server as by default there is no password for the root account):

  11. Hi r3dux,

    i tried to connect mysql db using c++. but, compile time i get no input file from to solve this problem?

  12. How can I specify connection parameters when creating a connection?
    For instance, I want to set the parameter “CharSet” to “utf8”.

    I’m used to appending all the key / value pairs after the port in the url, but that doesn’t seem to work.


  13. Hi. Thank you before nothing.

    I’m working with Windows 7 and Visual Studio 2010. I have the same problem that was writen by “rr.” user.

    I try connect to mysq data through c++ code. When I compile it in DEGUB mode, it works successfully, but I need compile my proyect with RELEASE mode. In this case always has an error to connect to dabase:

    ” dbConn = driver->connect(current_server, current_username, current_password); ”

    “Could not connect to database. Error message: Unknown exception”.

    I have tried with:

    MySQL Connector/C++ 1.1.1 (and boost_1_52 for this case to be neccesary)
    MySQL Connector/C++ 1.0.5

    Thanks in advance.

    1. I wish I knew how to help you – I’ve had nothing but headaches from trying to use MySQL Connector C++ with various versions of Visual Studio, Boost and compiling in Debug and Release mode. On one machine it’ll work fine, then on another it won’t work in one build mode but will in another, and on a different machine again it’ll work in the opposite mode (say, Debug) but not in Release!

      It (MySQL Connector C++) just seems to be problematic and poorly maintained piece of code all round.

      Maybe this guy can help?:

      Best of luck with it and sorry I couldn’t be of more help.

  14. Hi, i know that it is an old post but it has been very usefull for me. The problem comes when i try to query the database (the driver and the conexión are fine), i get this error:

    – SQL error. Error message: Statement has been closed
    sh: 1: pause: not found

    I have been looking for a solution but i am completly desperate. If i put the dbConn->setSchema command in order to select the database that i want to use, it throws other error:

    – SQL error. Error message: MySQL_Connection::prepareStatement(const sql::SQL String& sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)

    I am writting to you because i have alredy checked all solutions that you have given to the other messages.

    Thanks a lot for your help and excuse me for my poor english.

      1. I put here the code. You are going to see that I have imported openCv libraries. This is because i need the connection to the database for a computer vision project.
        Until the query of the database everything works fine so i suppose that is not a problem with the libraries. I have also disabled the firewall and i have checked (via phpmyadmin) that a database called “PFG” exists with a table called “AFORO”.
        Thanks for your help.

        1. Hmm, that’s some pretty straight-forward code, so I don’t see a lot to go wrong… Some possible issues:

          Line 35 – You’ve left the comment “no root password”, but you have a password for the root account which is ‘p’ – but… your connection to the database server doesn’t error so they must be the correct credentials.

          Lines 74 & 75 – Database and table names may be case-sensitive, try “pfg” and “aforo”.

          …I can’t see a lot else which may cause the issue – I don’t see the stmt object going out of scope or being closed anywhere, which makes me think perhaps you’re not getting any results at all…

          What happens when you execute “show tables” instead of “SELECT * FROM AFORO”? Do you get anything?

          As another option, you may like to try to make your code more like the code shown here:

          The code linked above does a lot more looking at the properties of the database and may help you to narrow down the problem, for example, if you can’t get ANY data about the database itself, then you’re not hooking into the database properly (or at least it’s not working).

          A final thing would be to get the absolute latest & greatest ConnectorC++ library (version 1.1.3 at the time of writing – if you’re using libraries provided with the sample project in this article. Or… maybe just try running the example project and see if that will hook in and “show tables”? The ConnectorC++ page says to try to use the same tools as used to compile ConnectorC++ itself – this may involve going up or down a version of VisualStudio (assuming you’re using visual studio – if not then you should try to get ConnectorC++ for the Linux distro you’re using or your Mac [libs for one platform won’t work on another platform, obviously].

          Can’t think of much else that would help you sorry – hope you get it sorted!


Leave a Reply

Your email address will not be published.