MySQL++ Windows Environment Configuration and Application Example: A suite of MySQL API s dedicated to C++.

What is MySQL++?

MySQL++ is a re-encapsulation of the MySQL CAPI to make processing SQL as easy as processing STL containers, since STL containers are treated as containers, iterators are used to process SQL statements and result sets.

Download MySQL++.

There are three things to prepare before installation:
1.MySQL database

mount this database

Tutorial Brief

Install MySQL CAPI

Normally, MySQL should be installed with several api packages (if I remember correctly), if you download MySQL CAPI separately, you can go to the official website and download this:

There are two important files, the header directory include and the link library file lib.

MySQL CAPI s can be used directly, but the operation is cumbersome, but you can write your own class to encapsulate it. Here is an example:


The note in the strip has Chinese scrambled code, which will be explained later. The meaning of the capi statement is not explained here.

#pragma once
#include <winsock.h>
using namespace std;
using table = vector<vector<string>>;
	//string host, user, password, databass, port;
	MYSQL mysql_id;
	void connect(string host, string user, string password, string database, int port);
	friend ostream& operator<< (ostream& os, table mytable);
	table execSql(string sql);
ostream& operator<< (ostream& os, table mytable);


#include "MYSQL_CAPI.h"
	if (mysql_init(&mysql_id) != NULL) {
		cout << "mysql_init() success" << endl;
	else {
		throw "mysql_init() failed";
	if (mysql_library_init(0, nullptr, nullptr) == 0) {
		cout << "mysql_library_init() success" << endl;
	else {
		throw "mysql_library_init() failed";
void MYSQL_CAPI::connect(string host, string user, string password, string database, int port)
	if (!mysql_real_connect(&mysql_id, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, nullptr, 0)) {
		throw "mysql_real_connect() failed";
	else {
		cout << "mysql_real_connect() success" << endl;
table MYSQL_CAPI::execSql(string sql)
	int error_no = mysql_errno(&mysql_id);
	string error_message = mysql_error(&mysql_id);
	if (error_no != 0) {
		throw "error_message:" + error_message;
	table table_result;//������
	MYSQL_RES *result = mysql_store_result(&mysql_id);
	if (result == nullptr) {
		return table();
	int col = mysql_num_fields(result);
	int row = mysql_num_rows(result);
	int i = 0;
	while (i < row) {
		auto row_result = mysql_fetch_row(result);
		for (int j = 0; j < col; j++) {
	return table_result;
ostream& operator<<(ostream& os, table table_result)
	for (auto& row : table_result) {
		for (auto& col : row) {
			os << col << "\t";
		os << endl;
	return os;


int main()
	while (true) {
		try {
			MYSQL_CAPI mysql1;
			string host = "localhost";
			string user = "root";
			string password = "123456";
			string database = "test";
			int port = 3306;
			mysql1.connect(host, user, password, database, port);
			string sql;
			cout << "mySQL>";
			getline(cin, sql);
			while (sql != "exit") {
				table mytable = mysql1.execSql(sql);
				cout << "---------------------" << endl;
				cout << mytable;
				cout << "---------------------" << endl;
				getline(cin, sql);
		catch (string e) {
			cout << e << endl;
	return 0;

Execution effect:

Download MySQL++.

Also go to the official website, download this thing:

The most important are the lib files, and the three project files.

Configuration environment

MySQL++ is based on MySQL CAPI, not MySQL connector C++.

1. Find the project file mysql++.sln in the MySQL++ folder under the vs2008 directory

2. Run with visual studio 2019, update, run error cannot find mysql.h

3. Configure environment variables for MySQL C

4. In the open project, mysqlpp runs first, so we only need to configure the environment of mysqlpp

Everything you can't find, go to the MySQL c api file to find, to link.

5. Compile, run, and generate lib and dll files in the same folder debug directory:

These two libraries are needed when using MySQL++. For convenience, we add them to the environment variable to get a separate folder. To see some, I don't take them here, just include the Debug file:

6. New Test Project:

Including these two, normally it would look nice to include the folder in mysql+, but for some reason, it's named after the lib file.
Add additional dependencies: mysqlpp_d.lib

7. Normally here, your MySQL++ is in place.

For example:

The MySQL++ website gives you two ways to manipulate SQL result sets:

1. Use SSQL structure

Write a structure and then define a structure container, official example:

vector<stock> v;
query << "SELECT * FROM stock";
for (vector<stock>::iterator it = v.begin(); it != v.end(); ++it) {
  cout << "Price: " << it->price << endl;

2. Use STL

Official examples:

vector<mysqlpp::Row> v;
query << "SELECT * FROM stock";
for (vector<mysqlpp::Row>::iterator it = v.begin(); it != v.end(); ++it) {
  cout << "Price: " << it->at("price") << endl;

I personally prefer to use the class operations provided by MySQL++, which is the second.
To give an example:

using namespace std;
using namespace mysqlpp;
int main()
	// Set up a connection
	string database, host, password, user;
	database = "material";
	host = "localhost";
	password = "123456";
	user = "root";
	unsigned int port = 3306;
	Connection connect(false);
	// connect.set_option(new SetCharsetNameOption("utf8"));
	if (connect.connect(database.c_str(), host.c_str(), user.c_str(), password.c_str(), port))
		cout << "connect succeed." << endl;
	Query query = connect.query();
	vector<Row> row;
	query << "SELECT * FROM p";
	int i = 0;
	for(vector<Row>::iterator it = row.begin(); it != row.end(); it++)
		cout << it->at(0) << "  ";
		cout << it->at(1) << "  ";
		cout << it->at(2) << "  ";
		cout << it->at(3) << endl;
	cout << "Chinese" << endl;
	return 0;

Explain sudden Chinese scrambling

Why is it sudden? When I used visual studio 2019, there was never any Chinese scrambling. The console character set of vs is Unicode, which is very compatible with Chinese.

But today when I operate the database, the Chinese language is out of order.

Plus, the CAPI s you've written before don't get messy, so the first thing to think about is whether it's normal to have a database setup problem, check the field types and character sets, and output tests in the MySQL console.

Then I naturally think about whether it's a problem with MySQL+. Just look at the source code, look at the variables, and find nothing wrong. Because MySQL++ encapsulates MySQL CAPI, I suspect it's a problem with CAPI again. But when I write an example of CAPI, it's denied because CAPI encapsulation is not high and does not change characters.

Then I just tried Chinese with a normal cout and found that it was garbled.

Install utf8 plug-in, No.

Changing the character set to a multi-character set and inheriting parent class is not possible.

The result is:

This setup problem in Windows10 was solved by scrambling after it was checked, but the previous Chinese language did become scrambled. It has never been changed. I don't know if it is the reason for the update of win10.

Tags: C++ Windows MySQL

Posted on Fri, 15 Oct 2021 12:11:22 -0400 by Smifffy