安装官方提供的mysqlconnect后,可以使用mysql++库,在官方的C API上再次做一个c++面向对象封装。
这里mysql++的安装依赖于mysql-connector-c。安装参考:http://dev.mysql.com/doc/connector-c/en/connector-c-installation-source-unix.html
c++11下注意:
1.由于mysql++并没有采用c++11,所以在使用的时候报了一些警告,需要对源码进行修改。
2.在获取字段类型的API在g++编译器下不是非常理想
3.执行sql存在错误时,系统会coredump,注意try语句的使用
下面部分代码:
1 #pragma once
2 #include "mysql++.h"
3
4 #define MYSQL_CFG_FILE "..//..//common//MySQL//config.json"
5
6
7 typedef struct mysql_config
8 {
9 std::string ip;
10 int port;
11 std::string user;
12 std::string passwd;
13 std::string database_name;
14 }mysql_cfg;
15
16 typedef struct mysql_field_information
17 {
18 std::string Field;
19 std::string Type;
20 std::string Null;
21 std::string Key;
22 std::string Default;
23 std::string Extra;
24 }mysql_field_info;
25
26 typedef std::vector<mysql_field_info> mysql_table_info;
27
28 typedef struct mysql_field_data
29 {
30
31 short SHORT;
32 int INTEGER;
33 long long BIGINT;
34 float FLOAT;
35 double DOUBLE;
36
37 std::string DATETIME;
38
39 std::string VARCHAR;
40 }mysql_field;
41
42 typedef std::map<std::string, std::vector<mysql_field>> mysql_table;
43
44 enum mysql_database_info
45 {
46 SHOW_VERISON,
47 SHOW_DATABASES,
48 SHOW_TABLES
49 };
50
51 class DMMySQL
52 {
53 public:
54 DMMySQL();
55 ~DMMySQL();
56
57 void show_databases_info(int flag, std::vector<std::string>& databases);
58
59 bool get_table_desc(std::string table_name, mysql_table_info& table_info);
60
61 bool insert_mysql(std::string sql);
62
63 bool insert_mysql(std::string table_name, std::map<std::string, std::string>& insert_data);
64
65 bool update_mysql(std::string sql);
66
67 bool update_mysql(std::string table_name, std::string field_name, std::string field_value,
68 std::string filter_key, std::string filter_value);
69
70 bool select_mysql(std::string table_name, mysql_table& table_data);
71
72 bool select_mysql(std::string table_name, std::string field_name, std::vector<mysql_field>& field_data);
73
74 bool select_mysql(std::string table_name, std::string field_name,
75 std::string filter_key, std::string filter_value,
76 std::vector<mysql_field>& field_data, std::string filter_opt = "=");
77
78 private:
79 bool load_mysql_config();
80
81 void init();
82
83 bool conncet_mysql();
84
85 void disconnect_mysql();
86
87 void trans_data_type(std::string input_data, std::string data_type, mysql_field& field_data);
88
89 private:
90 mysql_cfg _mysql_cfg;
91 mysqlpp::Connection _conn;
92 };
1 #include "json/json.h"
2 #include <fstream>
3 #include <ace/Log_Msg.h>
4 #include "DMMySQL.h"
5 #include <cxxabi.h>
6
7 #define TRY_SQL \
8 try{
9
10 #define CATCH_SQL_ERROR \
11 }\
12 catch(const mysqlpp::BadQuery& error)\
13 {\
14 ACE_DEBUG((LM_ERROR,"MySQL Query error:%s!\n",error.what()));\
15 return false;\
16 }\
17 catch (const mysqlpp::BadConversion& error)\
18 {\
19 ACE_DEBUG((LM_ERROR,"MySQL Conversion error:%s!\n",error.what()));\
20 return false;\
21 }\
22 catch (const mysqlpp::Exception& error)\
23 {\
24 ACE_DEBUG((LM_ERROR,"MySQL Exception error:%s!\n",error.what()));\
25 return false;\
26 }
27
28 DMMySQL::DMMySQL()
29 {
30 init();
31 }
32
33 DMMySQL::~DMMySQL()
34 {
35 disconnect_mysql();
36 }
37
38 bool DMMySQL::load_mysql_config()
39 {
40 std::ifstream cfg_file;
41 cfg_file.open(MYSQL_CFG_FILE, std::ios::binary);
42
43 if (!cfg_file.is_open())
44 {
45 return 0;
46 }
47
48 Json::Reader Reader;
49 Json::Value Root;
50
51 if (Reader.parse(cfg_file,Root))
52 {
53 _mysql_cfg.ip = Root["mysql_ip"].asString();
54 _mysql_cfg.port = Root["mysql_port"].asInt();
55 _mysql_cfg.user = Root["mysql_user"].asString();
56 _mysql_cfg.passwd = Root["mysql_passwd"].asString();
57 _mysql_cfg.database_name = Root["database_name"].asString();
58 }
59 else
60 {
61 ACE_DEBUG((LM_ERROR,"parse mysql config file failure!\n"));
62 return false;
63 }
64
65 return true;
66 }
67
68 void DMMySQL::init()
69 {
70 if (load_mysql_config())
71 {
72 conncet_mysql();
73 }
74 }
75
76 bool DMMySQL::conncet_mysql()
77 {
78 if (!_conn.connect(_mysql_cfg.database_name.c_str(), _mysql_cfg.ip.c_str(),
79 _mysql_cfg.user.c_str(), _mysql_cfg.passwd.c_str(), _mysql_cfg.port))
80 {
81 ACE_DEBUG((LM_ERROR,"connect MySQL database failure!\n"));
82 return false;
83 }
84
85 return true;
86 }
87
88 void DMMySQL::show_databases_info(int flag, std::vector<std::string>& databases)
89 {
90 std::string opration;
91
92 switch (flag)
93 {
94 case SHOW_VERISON:
95 {
96 std::string version_info = _conn.client_version();
97 databases.push_back(version_info);
98 return;
99 }
100 case SHOW_DATABASES:
101 {
102 opration = "show databases";
103 break;
104 }
105 case SHOW_TABLES:
106 {
107
108 opration = "show tables";
109 break;
110 }
111 default:
112 {
113 return;
114 }
115 }
116
117 mysqlpp::Query query = _conn.query(opration);
118 if (mysqlpp::StoreQueryResult res = query.store())
119 {
120 mysqlpp::StoreQueryResult::iterator rit;
121 for (rit = res.begin(); rit != res.end(); ++rit)
122 {
123 databases.push_back((*rit)[0].c_str());
124 }
125 }
126 }
127
128 bool DMMySQL::get_table_desc(std::string table_name, mysql_table_info& tbl_info)
129 {
130 TRY_SQL
131 std::string opration = "describe " + table_name;
132 mysqlpp::Query query = _conn.query(opration);
133 mysqlpp::StoreQueryResult res = query.store();
134
135 int field_num = res.size();
136 for (int i = 0; i < field_num; ++i)
137 {
138 mysql_field_info field_i;
139
140 field_i.Field = res[i]["field"].c_str();
141 field_i.Type = res[i]["type"].c_str();
142 field_i.Null = res[i]["null"].c_str();
143 field_i.Key = res[i]["key"].c_str();
144 field_i.Default = res[i]["default"].c_str();
145 field_i.Extra = res[i]["extra"].c_str();
146
147 tbl_info.push_back(field_i);
148 }
149 CATCH_SQL_ERROR
150 return true;
151 }
152
153 void DMMySQL::disconnect_mysql()
154 {
155 _conn.disconnect();
156 }
157
158 bool DMMySQL::insert_mysql(std::string sql)
159 {
160 TRY_SQL
161 mysqlpp::Query query = _conn.query(sql);
162 query.execute();
163 CATCH_SQL_ERROR
164 return true;
165 }
完整代码参考:https://github.com/binchen-china/DMServer