C + + uses MySQL's C API and the two ways to operate MySQL via MySQL's connector C + + 1.1.3
Operation with vs2013 and 64-bit mSQL 5.6.16
The database used in the project is named Booktik
Table for Book
..........
(Total 30 records, only partial records, 14-30 not listed)
First, the C API through the MySQL operation
1. Create a new empty project
2. Add D:\Program files\mysql\mysql Server 5.6\include to the project's include directory (depending on the path)
3. Add D:\Program files\mysql\mysql Server 5.6\lib to the project's library directory (depending on the path)
4. Add Libmysql.lib to Additional dependencies
(*3.4 Step can also be added at the beginning of the program code #pragma comment (lib, "D:\\Program files\\mysql\\mysql Server 5.6\\lib\\libmysql.lib") To import Libmysql.lib)
5, if the use of MySQL is 64-bit, you also need to change the project's solution platform from Win32 to x64
6. Copy the Libmysql.dll from the D:\Program files\mysql\mysql Server 5.6\lib (depending on the path) to the project, and the. cpp,.h file is located under the same path
At this point, all the relevant configurations are complete
Program code
Main.cpp
#include <Windows.h>#include <mysql.h>#include <String>#include <iostream>UsingNamespaceStd#pragma comment (lib, "D:\\Program files\\mysql\\mysql Server 5.6\\lib\\libmysql.lib")IntMain () {ConstChar user[] ="Root";ConstChar pswd[] ="123456";ConstChar host[] ="localhost";ConstChar table[] ="Booktik"; Unsignedint port =320W; MYSQL Mycont; Mysql_res *Result Mysql_row Sql_row;IntRes Mysql_init (&Mycont);if (Mysql_real_connect (&mycont, host, user, pswd, table, port, NULL,0) {mysql_query (&mycont,"SET NAMES GBK");//Set the encoding format res = mysql_query (&mycont,"SELECT * FROM book");//Inquireif (!RES) {result = Mysql_store_result (&Mycont);If(Result) {while (Sql_row = mysql_fetch_row (Result))//Get the specific data{cout<<"BookName:"<< sql_row[1] <<Endl cout<<"SIZE:"<< sql_row[2] <<Endl } } }Else {cout << query SQL failed! "<< Endl;}} else {cout << connect failed! "<< Endl;} if (Result!=mycont); system ( " pause); return 0
The results of the operation are as follows:
Second, through the MySQL connector C + + 1.1.3 to operate
The implementation of MySQL C + + driver is based on the JDBC specification MySQL connector/c++ is a MySQL connector developed by Sun Microsystems. It provides an OO-based programming interface with a database driver to operate the MySQL server.Unlike many other existing C + + interface implementations, connector/c++ follows the JDBC specification. That is, the API for connector/c++ driver is primarily a JDBC interface based on the Java language. JDBC is the standard industrial interface between the Java language and various database connections. Connector/c++ implements most of the JDBC specifications. If the developer of the C + + program is familiar with JDBC programming, it will be very quick to get started.
MySQL connector/c++ need to install configuration boost Library, boost library installation compilation is not elaborated here
1. Create a new empty project
2. Add D:\Program files\mysql\connector C + + 1.1.3\include to the project's inclusion directory (depending on the path)
3. Add D:\boost\boost_1_55_0 to the project's include directory (depending on the path)
4. Add D:\Program files\mysql\connector C + + 1.1.3\lib\opt to the project's library directory (depending on the path)
5. Add Mysqlcppconn.lib to Additional dependencies
6, if the use of MySQL is 64-bit, you also need to change the project's solution platform from Win32 to x64
7, the D:\Program files\mysql\connector C + + 1.1.3\lib\opt (depending on the specific path) under the Mysqlcppconn.dll copy to the project, and. cpp,.h files are located under the same path
Copy the Libmysql.dll from the D:\Program files\mysql\mysql Server 5.6\lib (depending on the path) to the project, and the. cpp,.h file is located under the same path
At this point, all the relevant configurations are complete
Program code
Main.cpp
#include <iostream>#include <map>#include <String>#include <memory>#include"Mysql_driver.h"#include"Mysql_connection.h"#include"Cppconn/driver.h"#include"Cppconn/statement.h"#include"Cppconn/prepared_statement.h"#include"Cppconn/metadata.h"#include"Cppconn/exception.h"UsingNamespaceStdUsingNamespacesqlIntMain () {Sql::mysql::mysql_driver *driver =0; Sql::connection *conn =0;Try{Driver =Sql::mysql::get_mysql_driver_instance (); conn = Driver->connect ("Tcp://localhost:3306/booktik","Root","123456"); cout <<"Connection Successful"<<Endl }Catch(...) {cout <<"Connection failed"<<Endl } sql::statement* stat = conn->Createstatement (); Stat->execute ("Set names ' GBK '"); ResultSet *Res res = Stat->executequery ("SELECT * FROM book");while (res->Next ()) {cout << "bookname:" << Res->getstring ( "bookname Endl; cout << " Size:" << res->getstring ( "size ") << Endl;} IF (conn! = 0 "pause" );}
C + + operation MySQL Method summary (1) [reprint]