vc++2010 Configuration using MySQL5.6

Source: Internet
Author: User

0. Prerequisites

Post-installation files overview

Compiler: VC++2010

MySQL version: MySQL5.6.29 for Win64

Connector version: Connector C + + 1.1.3

Configuring the use of MySQL under VS2010

1. Configuration header File

Project Properties--vc++ Directory--Include directory

2. Configuration library file

There are two directories in the connector C + + 1.1.3\lib directory: Debug directory and opt directory

Lib\debug Directory

Lib\opt Directory

Because of the debug directory, so guess the OPT directory may be similar to the Release Directory optimization (optimize) after the file, so in VC + + use in the Debug directory under Debug library files, in release mode using the OPT directory in the library directory.

eg.

#ifdef _DEBUG

#pragma comment (lib, "Mysqlcppconn.lib underdebug ")

#pragma comment (lib, "Mysqlcppconn-static.lib underdebug ")

#else

#pragma comment (lib, "Mysqlcppconn.lib underopt ")

#pragma comment (lib, "Mysqlcppconn-static.lib underopt ")

#endif

Also, copy the Mysqlcppcon.dll in the debug or opt directory to the project directory or the system32 directory under Debug or Release mode. Copy the Libmysql.dll under the Mysql\mysql server5.6\lib directory to the project directory or the System32 directory.

3. Configuration Items

Since this version of MySQL is 64-bit, the project that uses the MySQL connector must be configured for the X64 type. Otherwise there will be a link error! Pay attention to this point!

4. Demo

Database: db_1220, table: Tbl_user, MySQL server: local localhost

#include"stdafx.h"#include<Windows.h>#include<iostream>#include<mysql_connection.h>#include<cppconn/driver.h>#include<cppconn/exception.h>#include<cppconn/resultset.h>#include<cppconn/statement.h>#pragmaWarning (disable:4251)#ifdef _DEBUG#pragmaComment (lib, "D:\\Program files\\mysql\\connector C + + 1.1.3\\lib\\debug\\mysqlcppconn-static.lib")#pragmaComment (lib, "D:\\Program files\\mysql\\connector C + + 1.1.3\\lib\\debug\\mysqlcppconn.lib")#else#pragmaComment (lib, "D:\\Program files\\mysql\\connector C + + 1.1.3\\lib\\opt\\mysqlcppconn-static.lib")#pragmaComment (lib, "D:\\Program files\\mysql\\connector C + + 1.1.3\\lib\\opt\\mysqlcppconn.lib")#endifusing namespacestd;int_tmain (intARGC, _tchar*argv[]) {sql::D River*driver =NULL; Sql::connection*con =NULL; Sql::statement*STMT =NULL; Sql::resultset*res =NULL; Sql::sqlstring Strhost ("localhost"); Sql::sqlstring struser ("Root"); Sql::sqlstring Strpwd ("XXXXXXX"); Sql::sqlstring Strschema ("db_1220"); Sql::sqlstring strquery ("SELECT * from Tbl_user"); Try{driver=get_driver_instance (); Con= driver->Connect (strhost, struser, strpwd); Con-Setschema (Strschema); stmt= con->createstatement (); Res= stmt->executeQuery (strquery); Sql::resultsetmetadata* Pmetadata = res->GetMetaData (); cout<<Endl; cout<<"Results has"<< Res->rowscount () <<"rows"<< Endl <<Endl;  while(res->Next ()) {            //get data by column namecout << Res->getint ("ID")                 <<"   "<< res->getstring ("name"). C_STR ()//Sql::sqlstring does not overload the << operator, so it cannot be cout<<res->getstring directly ("name")<<"   "<< res->getstring ("Password"). C_str ()<<Endl; //get data by column indexcout << Res->getint (1)                 <<"   "<< res->getstring (2). C_str ()<<"   "<< res->getstring (3). C_str ()<<Endl; }    }    Catch(sql::sqlexception&e) {Cerr<< Endl << e.what () <<Endl; }        Catch (...) {Cerr<< Endl <<"some exception happeded"<<Endl; }    if(NULL! =res) Delete res; if(NULL! =stmt) Delete stmt; if(NULL! =con) Delete con; cout<< Endl <<Endl; return 0;}

Operation Result:

5. Supplement

If the error message like "<boost/variant.hpp>" is not found during compilation, it is necessary to support the Boost library and download the boost library configuration.

vc++2010 Configuration using MySQL5.6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.