Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Using MySQL Connector/C++ with Visual Studio 2010

  Pi Ke        2012-06-01 13:52:20       30,530        1    

MySQL Connector/C++ is one of the latest connectors for MySQL, developed by Sun Microsystems. The MySQL connector for C++ provides an object-oriented application programming interface (API) and a database driver for connecting C++ applications to the MySQL Server.

When we want to build a database application with C++ using Visual Studio 2010, we may use the MySQL Connector/C++, but sometimes we may encounter different errors, either compile or runtime error. After tried many times, I finally made my C++ program successfully connect to MySQL database with Visual Studio 2010. Here I share what I have done..

Prerequisites

  • Visual Studio 2010
  • MySQL Server 5.1.x

Note : MySQL Connector/C++ 1.1.0 needs boost libraries to work correctly, while MySQL Connector/C++ 1.0.5 doesn't need boost, but it causes runtime error when reading data from database. So we choose MySQL Connector/C++ 1.1.0 here .

Steps.

1. Download all the necessaries files and libraries. We assume you have already installed Visual Studio 2010. For MySQL Server, you can download the msi installer and install it . Also you can download the MySQL Connector/C++ 1.1.0 msi file and install it.

2. Then what you need to do is to open Visual Studio 2010 and create a new C++ Win32 Console project. Then you can create a C++ file and copy the the example code from the Developing Database Applications Using MySQL Connector/C++.

3. Now you need to configure the project properties, Go to Project menu and open the properties window, you need to modify some places:

  • VC++ Directories->Include Directories->Add the path of your MySQL Connector/C++ include folder
  • VC++ Directories->Include Directories->Add the path of your MySQL Connector/C++ include/cppconn folder
  • VC++ Directories->Include Directories->Add the path of your boost library folder (for me its (D:\Projects\C++ Projects\boost_1_49_0)
  • VC++ Directories->Library Directories->Add the path of your MySQL Connector/C++ lib/opt folder
  • Linker->Input->Additional Dependencies->Add "mysqlcppconn.lib"

4. Then you should change respective database settings in the source file such as host, username and password and database name. 

5. Now build the solution and it should be built successfully.

Some errors you may encounter during the build process are:

  1. error C1083: Cannot open include file: 'sqlstring.h': No such file or directory    d:\projects\c++ projects\lib\mysql_vs2010\include\cppconn\warning.h-->Solution : You need to download a MySQL Connector/C++ zip version from MySQL website because MySQL Connector/C++ msi version missed a header file named "sqlstring.h", you need copy the "sqlstring.h" file to the installed MySQL Connector/C++ include/cppconn folder.
  2. error C2371: 'int8_t' : redefinition; different basic types    d:\projects\c++ projects\lib\mysql_vs2010\include\cppconn\config.h--> Solution : You can comment the line typedef __int8            int8_t; in MySQL Connector/C++ include\cppconn\config.h file
  3. Error : unresolved external symbol __imp__get_driver_instance. -->Solution : This problem happens because the linker cannot find the mysqlcppconn.lib. You should do this "Project->[You project name] properties->Linker->Input->Additional Dependencies->Add mysqlcppconn.lib"

If you still have problems to make the MySQL Connector/C++ work with Visual Studio 2010. We can discuss together. Please comment below or email me : pikexxn@gmail.com. If you find any mistakes I made here, please feel free to let me know.

Update

"get_driver_instance() is now only available in dynamic library builds - static builds do not have this symbol. This was done to accommodate loading the DLL with LoadLibrary or dlopen."

C++  VS2010  MYSQL CONNECTOR  VISUAL STUDIO 2010 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  1 COMMENT


Anonymousf [Reply]@ 2017-06-28 05:44:48

worst