Linux mysql c API封装
来源:互联网 发布:软件运维工程师 知乎 编辑:程序博客网 时间:2024/06/05 08:53
mysql 是目前使用量最大的关系型数据库,在互联网公司应用非常广泛,在一些非高并发场景下应用MySQL能大幅降低开发运维和部署的成本。
下面是自己封装的c++ 风格的MySQL客户端,相对原生的c api 使用相对更加简便一些
/******************************************************function: mysql c api wrapp for c++ style.author: liuyidate: 2016.04.13version: 1.0******************************************************/#ifndef MYSQL_CONNECTOR#define MYSQL_CONNECTOR#include <stdio.h>#include <iostream>#include <string>#include <vector>#include <mysql/mysql.h>using namespace std;class mysql_connector{public:mysql_connector(){}~mysql_connector(){mysql_close(&m_mysql);}bool init(const string& host, const string& user, const string& pw, const string& db, int port=3306, const string& code="utf8"){m_host = host;m_user = user;m_pw = pw;m_db = db;m_port = port;m_code = code;return NULL != mysql_init(&m_mysql) && connect() && execute_sql(("set names " + m_code).c_str());}bool reconnect(){close_connect();return connect() && execute_sql(("set names " + m_code).c_str());}void close_connect(){mysql_close(&m_mysql);}bool execute_sql(const string& sql){return 0 == mysql_real_query(&m_mysql, sql.c_str(), sql.size());}bool select_data(const string& sql, vector<vector<string> >& result_matrix){if(!execute_sql(sql))return false;MYSQL_RES *res = mysql_store_result(&m_mysql);if(NULL == res){ mysql_free_result(res);return false;}int numcols = mysql_num_fields(res);MYSQL_ROW row;while(row = mysql_fetch_row(res)){vector<string> row_vec;row_vec.reserve(numcols);for(int i = 0; i < numcols; i++){row_vec.push_back(row[i]);}result_matrix.push_back(row_vec);}mysql_free_result(res);return true;}string get_error_msg(){char buf[2048] = {0};snprintf(buf, 2047, "error_code=%d, error_msg=%s", mysql_errno(&m_mysql), mysql_error(&m_mysql)); return buf;}private:bool connect(){return NULL != mysql_real_connect(&m_mysql, m_host.c_str(), m_user.c_str(), m_pw.c_str(), m_db.c_str(), m_port, NULL, 0);}private: MYSQL m_mysql; string m_host; string m_user; string m_pw; string m_db; string m_code; int m_port;};#endif
#include "mysql_connector.h"int main(int argc, char* argv[]){mysql_connector conn;cout<<conn.init("127.0.0.1", "newborn", "newbornmysqladmin", "bbloves", 3306, "utf8")<<endl;cout<<conn.reconnect()<<endl;cout<<conn.get_error_msg()<<endl;vector<vector<string> > v;cout<<conn.select_data("select cellphone, mother_name, report_id from report where msg_notify_count < 2", v)<<endl;for(size_t i = 0 ; i < v.size(); i++){for(size_t j = 0; j < v[i].size(); j++)cout<<v[i][j]<<endl;cout<<"---------------"<<endl;}cout<<conn.get_error_msg()<<endl;return 0;}
g++ test_mysql.cpp -L/usr/lib64/mysql -lmysqlclient -lpthread
0 0
- Linux mysql c API封装
- C++封装MySQL C API
- 封装MySQL C API 基本操作
- C++封装MySQL预处理C API
- MYSQL C API 封装和MYSQL连接池
- 自已封装c api mysql--connection.hpp(初步)
- 简单易用的mysql c api 封装类
- C++封装 mysql C api遇到的问题
- Linux下使用MySQL C API
- Linux-(C)Mysql API编程学习
- java的mysql api封装
- MySQLWrapper(推荐一款MySQL C API封装类库)v.0.4
- Mysql数据库学习(四):常用Mysql C API 介绍和使用、封装一个访问Mysql数据库的类MysqlDB
- Linux下用C语言API连接MySQL数据库
- Linux下用C语言API连接MySQL数据库
- Linux下C语言API连接MySQL数据库编译选项
- Linux 使用MySQL C API查询出现中文乱码
- Linux下用C语言API连接MySQL数据库
- torque--客户端使用torque
- 【毕业生推荐系统】(2) 需求与数据库设计
- php集成环境总结(php新手)
- 设计模式简要总结——行为型模式
- 异步请求引发的Chrome死锁
- Linux mysql c API封装
- volatile关键字和mutable关键字
- Linux内核学习笔记八——定时器和时间管理
- Android系列--DOM、SAX、Pull解析XML
- torque---添加计算节点
- Java static关键字
- 中国科学院研制的开放式超导磁共振成像磁体系统
- ios开发-- 开发细节(得到当前View的ViewController)
- 极光单独设置铃声,震动模式相关问题解决