本文共 5829 字,大约阅读时间需要 19 分钟。
cmake
set(CMAKE_CXX_FLAGS "-g -lmysqlclient -pthread -lz -lm -lrt -ldl $CMAKE_CXX_FLAGS")
g++ build
g++ -o mysql -lmysqlclient -I/usr/include/mysql -L/usr/lib/x86_64-linux-gnu -pthread -lz -lm -lrt -ldl -g main.cc
小提示:
示例代码中使用到了另外两个三方库, 一个三方库时sspdlog
#include <sspdlog/sspdlog.h>
可以替换成 #include <iostream>
,
SSPD_LOG_INFO
可以替换为std::cout
, SSPD_LOG_ERROR
可以替换为std::cerr
另一个三方库 为 自己的 工具库 , 可以到github上下载
#include#include #include namespace mysql { struct library { library(int argc, char **argv, char **groups) { if(mysql_library_init(argc, argv, groups)) { throw std::runtime_error("MYSQL Initialize failed"); } } ~library() { mysql_library_end(); } }; struct row { row(MYSQL_ROW row): m_row(row) { } ~row() { } operator bool() const { return m_row; } std::string operator[](std::size_t index) const { return m_row[index]; } row(const row&) = delete; row(row&& other): m_row(other.m_row) { other.m_row = nullptr; } private: MYSQL_ROW m_row{ nullptr}; }; struct field { field(MYSQL_FIELD *field): m_field(field) { } std::string name() const { return m_field->name; } std::string catalog() const { return std::string(m_field->catalog, m_field->catalog_length); } ~field() { } field(const field&) = delete; field(field&& other): m_field(other.m_field) { other.m_field = nullptr; } protected: MYSQL_FIELD *m_field{ nullptr}; }; struct fields : field { fields(MYSQL_FIELD *f) : field(f) { } field operator[](std::size_t index) { return m_field + index; } }; struct result { result() = default; result(MYSQL_RES *res): m_result(res) { } ~result() { if(m_result) { ::mysql_free_result(m_result); } } result(const result&) = delete; result(result&& other): m_result(other.m_result) { other.m_result = nullptr; } row fetch_row() { return mysql_fetch_row(m_result); } std::uint64_t rows() const { return mysql_num_rows(m_result); } unsigned field_count() const { return ::mysql_num_fields(m_result); } fields fetch_fields() { return mysql_fetch_fields(m_result); } private: MYSQL_RES *m_result { nullptr}; }; struct client { client() : m_client(mysql_init(nullptr)) { } bool good() const { return nullptr != m_client; } bool not_good() const { return nullptr == m_client; } bool connect(const std::string& host, const std::string& user, const std::string& password, const std::string& database, unsigned short port = 3306) { return mysql_real_connect(m_client, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, nullptr, 0) != nullptr; } bool query(const std::string& sql) { return mysql_real_query(m_client, sql.c_str(), sql.length()) == 0; } std::uint64_t affected_rows() const { return mysql_affected_rows(m_client); } std::uint64_t inserted_id() const { return mysql_insert_id(m_client); } result use_result() const { return result(mysql_use_result(m_client)); } ~client() { } std::string error() const { return mysql_error(m_client); } private: MYSQL *m_client; };}int main(int argc, char**argv) try { mysql::library library(argc, argv, nullptr); mysql::client client{ }; if(client.not_good()) { SSPD_LOG_ERROR << "Create MySQL client failed"; return -1; } if(!client.connect("172.17.0.2", "blog", "blog.123", "blog")) { SSPD_LOG_ERROR << "MySQL connect failed: " << client.error(); return -2; } if(!client.query("INSERT INTO user set name='blog', email='blog@hotmail.com'")) { SSPD_LOG_ERROR << "MySQL query INSERT failed: " << client.error(); return -3; } SSPD_LOG_INFO << "MySQL affected rows after insert: " << client.affected_rows(); SSPD_LOG_INFO << "MySQL last inserted ID: " << client.inserted_id(); if(!client.query("SELECT id, name, email FROM user")) { SSPD_LOG_ERROR << "MySQL query SELECT failed: " << client.error(); return -3; } auto result = client.use_result(); auto field_count = result.field_count(); auto fields = result.fetch_fields(); while (auto row = result.fetch_row()) { for(auto i = 0; i < field_count; ++i) { SSPD_LOG_INFO << fields[i].name() << ": " << row[i]; } } if(!client.query(oyoung::format("DELETE FROM user WHERE id=%1").arg(client.inserted_id()).to_string())) { SSPD_LOG_ERROR << "MySQL query DELETE failed: " << client.error(); return -3; } SSPD_LOG_INFO << "MySQL affected rows after delete: " << client.affected_rows(); return 0;} catch(const std::exception& e) { SSPD_LOG_ERROR << e.what();}
转载地址:http://guagj.baihongyu.com/