官方API地址:https://www.mysqlzh.com/api/19.html
一、准备工作:
先下载MySQL对应的库文件,可以参考 https://www.cnblogs.com/Brickert/p/16167115.html
将mysql-5.7.37-win32下的 include、lib两个文件夹拷贝到vs工程文件夹。(适用于x86程序)
包含静态库:
#pragma comment(lib, "mysql/lib/libmysql.lib")
包含头文件:
#include "include/mysql.h"
包含动态库:
将libmysql.dll拷贝到生成exe的目录中
二、代码实例
#include <winsock.h> #include "mysql/include/mysql.h" #include <iostream> #include <string> #pragma comment(lib,"mysql/lib/libmysql.lib") using namespace std; int main() { cout << __FUNCTION__ << " is called." << endl; string sql; MYSQL mysql; try { mysql_init(&mysql); // 连接远程数据库 if (NULL == mysql_real_connect(&mysql, "192.168.20.239", "root", "123456", "mysql", 3306, NULL, 0)) { cout << __LINE__ << mysql_error(&mysql) << mysql_errno(&mysql) << endl; throw - 1; } //创建数据库test_db sql = "CREATE DATABASE if not exists test_db;"; if (mysql_query(&mysql, sql.c_str())) { cout << "line: " << __LINE__ << ";"<< mysql_error(&mysql) << mysql_errno(&mysql) << endl; throw - 1; } //进入数据库test_db sql = "use test_db;"; if (mysql_query(&mysql, sql.c_str())) { cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl; throw - 1; } //创建表test_table sql = "CREATE TABLE if not exists `test_table`(\ `id` INT auto_increment,\ `title` VARCHAR(100),\ `name` VARCHAR(100),\ primary key(id))\ default charset = utf8;"; if (mysql_query(&mysql, sql.c_str())) { cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl; throw - 1; } //插入数据,事务 sql = "begin;"; mysql_query(&mysql, sql.c_str()); sql = "INSERT INTO test_table(title,name)\ values (\"Sunday\",\"mind\");"; if (mysql_query(&mysql, sql.c_str())) { cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl; } sql = "commit;"; mysql_query(&mysql, sql.c_str()); //更新数据 sql = "UPDATE test_table set title = 'huang' where id < 3;"; if (mysql_query(&mysql, sql.c_str())) { cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl; } //删除数据 sql = "DELETE FROM test_table where id > 4;"; if (mysql_query(&mysql, sql.c_str())) { cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl; } //查询数据 sql = "SELECT * FROM test_table;"; if (mysql_query(&mysql, sql.c_str())) { cout << "line: " << __LINE__ << ";" << mysql_error(&mysql) << mysql_errno(&mysql) << endl; throw -1; } else { std::vector<std::map<std::string, std::string>>* vec_info; //读取检索的结果 MYSQL_RES *result = mysql_use_result(m_mysql); // if (result != NULL) { int num_fields = mysql_num_fields(result); //每一行的字段数量 MYSQL_ROW row; MYSQL_FIELD *fields = mysql_fetch_fields(result); //取列名 if (fields != NULL) { while (row = mysql_fetch_row(result)) //取每行值 { std::map<std::string, std::string> map_data; if (row != NULL) { for (int i = 0; i < num_fields; ++i) { map_data[fields[i].name] = row[i]; //组合数据 } vec_info->push_back(map_data); } } } } mysql_free_result(result); //调用mysql_use_result()后,必须调用mysql_free_result()释放结果集使用的内存 } } catch (...) { cout << "MySQL operation error!" << endl; } mysql_close(&mysql); system("pause"); return 0; }