c++使用mysqlclient库开发mysql

使用libmysqlclient库对mysql进行c++开发

安装

bash 复制代码
sudo apt update && sudo apt install libmysqlclient-dev -y

封装客户端

一般都是封装一个客户端类进行开发,如下的mysql.h:

cpp 复制代码
#pragma once
#include <mysql/mysql.h>
#include <string>
#include <iostream>

// 数据库操作类
class MySQL {
public:
    MySQL(); // 初始化数据库连接资源
    ~MySQL(); // 释放数据库连接资源
    bool connect(); // 连接数据库
    bool update(std::string sql); // 更新(insert, delete, update都是这个接口)
    MYSQL_RES *query(std::string sql); // 查询操作
    MYSQL* getConnection();     // 获取连接
private:
    MYSQL *_conn; //一条连接
};

mysql.cpp如下:

cpp 复制代码
#include "mysql.h"

// 数据库配置信息
static std::string server = "127.0.0.1";
static uint16_t port = 3306;
static std::string user = "root";
static std::string password = "123456";
static std::string dbname = "chat";

// 初始化数据库连接资源
MySQL::MySQL() {
    _conn = mysql_init(nullptr);
}

// 释放数据库连接资源
MySQL::~MySQL() {
    if (_conn) {
        mysql_close(_conn);
    }
}

// 连接数据库
bool MySQL::connect() {
    MYSQL *p = mysql_real_connect(_conn, server.c_str(), user.c_str(), password.c_str(), dbname.c_str(), port, nullptr, 0);
    if (p) {
        // C和C++代码默认的编码字符是ASCII,如果不设置,从MySQL上拉下来的中文会乱码
        mysql_query(_conn, "set names gbk");
        std::cout << "connect mysql success!" << std::endl;
    } 
    else {
        std::cout << "connect mysql failed!" << std::endl;
    }
    return p; //空不就是false吗
}

// 更新(insert,delete,update都是这个接口)
bool MySQL::update(std::string sql) {
    if (mysql_query(_conn, sql.c_str())) {
        std::cout << __FILE__ << ":" << __LINE__ << ":" << sql << "更新失败!" << std::endl;
        return false;
    }
    std::cout << __FILE__ << ":" << __LINE__ << ":" << sql << "更新成功!" << std::endl;
    return true;
}

// 查询操作
MYSQL_RES *MySQL::query(std::string sql) {
    if (mysql_query(_conn, sql.c_str())) {
        std::cout << __FILE__ << ":" << __LINE__ << ":" << sql << "查询失败!" << std::endl;
        return nullptr;
    }
    return mysql_use_result(_conn);
}

// 获取连接
MYSQL* MySQL::getConnection() {
    return _conn;
}

使用示例

可参考https://github.com/1412771048/chatserver/tree/main

cpp 复制代码
#include "mysql.h"
#include <iostream>

int main() {
    MySQL mysql;
    if (mysql.connect()) {
        char sql[1024] = {0};
            snprintf(sql, sizeof(sql), "select name from user where name='%s'", name.c_str());
            mysql.query(sql);
    } 
}

编译:

cpp 复制代码
g++ 1.cpp mysql.cpp -lmysqlclient
相关推荐
森G2 小时前
64、完善聊天室程序(TLV拓展)---------网络编程
网络·c++·tcp/ip
郝学胜-神的一滴3 小时前
完全二叉树与堆底层原理深度剖析 | 手写C++大顶堆实现
java·开发语言·数据结构·c++·python·算法
大白话_NOI3 小时前
【洛谷 P2678】 [NOIP2015 提高组] 跳石头 超详细题解
c++·算法
chase_my_dream4 小时前
LeGO-LOAM 详细源码流程解读
c++·计算机视觉·自动驾驶
插件开发4 小时前
vs2015 cuda c++ 线程号的计算详解
开发语言·c++·算法
有点。4 小时前
C++(前缀和与差分)
c++·算法
c++之路5 小时前
Bazel C++ 构建系列文档(五):多目标与多包项目
java·开发语言·c++
Hello:CodeWorld5 小时前
【C++ 避坑指南】告别缓冲区溢出!全面解析 std::snprintf 的安全美学与核心陷阱
开发语言·c++·安全
凡人叶枫5 小时前
Effective C++ 条款38:通过复合塑模出 has-a 或 \“根据某物实现出\
linux·开发语言·c++·windows
凡人叶枫5 小时前
Effective C++ 条款40:明智而审慎地使用多重继承
java·数据库·c++·嵌入式开发·effective c++