C + + operation MySQL Method summary (1)

Source: Internet
Author: User
Tags win32

Several methods of MySQL database for C + + operation are listed.

Two ways to operate with MySQL's C API and through 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 an 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>using namespacestd;#pragmaComment (lib, "D:\\Program files\\mysql\\mysql Server 5.6\\lib\\libmysql.lib")intMain () {Const CharUser[] ="Root"; Const CharPswd[] ="123456"; Const CharHost[] ="localhost"; Const CharTable[] ="Booktik"; unsignedintPort =3306;    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 formatres = mysql_query (&mycont,"SELECT * from book");//Enquiry        if(!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! =NULL) mysql_free_result (result); Mysql_close (&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 an empty project

2. Add D:\Program files\mysql\connector C + + 1.1.3\include to the project's include directory (depending on the MySQL installation path)

3. Add D:\boost\boost_1_55_0 to the project's include directory (depending on the MySQL installation 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"using namespacestd;using namespaceSQL;intMain () {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) {Delete conn; } System ("Pause");}

Run results

Related Article

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.