当前位置:   article > 正文

C++ MySql驱动_mysql c++驱动

mysql c++驱动

 MySQL数据库的优势包括:

1. 开源免费:MySQL是一款开源的关系型数据库管理系统,可以免费使用和修改。
2. 易用性:MySQL提供了简单直观的命令行界面和图形化界面,使得用户能够轻松地创建、查询和管理数据库。
3. 可靠性:MySQL具有良好的稳定性和可靠性,能够处理大规模数据和高并发访问。
4. 高性能:MySQL采用了多种优化策略和技术,如索引、缓存、分区等,以提供快速的读写操作和响应时间。
5. 扩展性:MySQL支持水平扩展和垂直扩展,可以方便地增加或减少服务器的数量、内存和存储容量,以满足不断增长的需求。
6. 兼容性:MySQL遵循SQL标准,与其他常见的数据库系统兼容,能够无缝迁移和集成现有的数据和应用程序。
7. 安全性:MySQL提供了灵活而强大的安全特性,如用户权限控制、SSL加密通信等,保护敏感数据的安全性。
8. 社区支持:MySQL拥有庞大的开源社区,用户可以通过社区获取帮助、分享经验和参与开发,获得及时的技术支持。

MySQL真可谓是集万千宠爱于一身啊,只可惜对于初学者而言,门槛似乎高了那么一点点,这里我针对MySQL的常用语法汇总写了这样一个库文件(支持跨平台使用),方便大家使用:

接口声明:

  1. /************************************************************************/
  2. /* MySQL_Driver MySQL_Connection */
  3. /* 利用MySql API接口,将其进行封装,旨在提供一个方便、快捷的访问接口 */
  4. /* 作者:老狼 */
  5. /* 时间:2023/08/26 */
  6. /* 版本:1.0 */
  7. /************************************************************************/
  8. #ifndef MYSQL_DRIVER_H
  9. #define MYSQL_DRIVER_H
  10. #include <string>
  11. #include <map>
  12. #include <vector>
  13. #include <iostream>
  14. #include "mysql.h"
  15. using namespace std;
  16. typedef std::map<std::string, std::string> FieldsValue;
  17. class MySQL_Driver
  18. {
  19. protected:
  20. //代表一个到数据库的连接
  21. MYSQL *mysql;
  22. //操作的结果集
  23. MYSQL_RES *dataSet;
  24. //返回查询得到的结果数
  25. my_ulonglong num;
  26. //错误提示信息
  27. std::string error;
  28. //查询语句返回一条结果
  29. FieldsValue record;
  30. //查询结果集
  31. std::vector<FieldsValue> records;
  32. //查询表结构的字段名称
  33. std::vector<std::string> fields;
  34. public:
  35. MySQL_Driver(void);
  36. virtual ~MySQL_Driver(void);
  37. // 连接驱动
  38. unsigned int connect(std::string host, std::string database, std::string user, std::string password, unsigned int port=3306);
  39. //执行sql语句
  40. unsigned int exec(std::string sql);
  41. //返回查询得到的结果
  42. std::vector<FieldsValue> fieldValues();
  43. //返回错误信息
  44. std::string errorCode();
  45. //返回查询表结构字段名称
  46. std::vector<std::string> fieldsName();
  47. //向数据库中插入一条数据
  48. unsigned int insert(std::string table, FieldsValue *data);
  49. //返回最后一个自动增量的值
  50. unsigned long getlastid();
  51. //返回一条sql语句影响的行数
  52. unsigned long numRowsAffected();
  53. //根据条件修改一条数据
  54. unsigned int update(std::string table, FieldsValue *data, std::string condition);
  55. //根据条件删除数据
  56. unsigned int remove(std::string table, std::string condition);
  57. //判断数据库是否连接
  58. bool connected();
  59. //断开数据库连接
  60. void disconnected();
  61. };
  62. class MySQL_Connection
  63. {
  64. public:
  65. MySQL_Connection(MySQL_Driver* pMysqlDriver);
  66. virtual ~MySQL_Connection(void);
  67. //执行sql语句
  68. bool exec(std::string sql);
  69. //取字符串值
  70. bool stringValue(std::string &str, std::string fieldName,int nFieldLen=-1);
  71. //取整型值
  72. bool intValue(long &lVal, std::string fieldName);
  73. //取浮点型值
  74. bool floatValue(double &lVal, std::string fieldName);
  75. //返回一条sql语句影响的行数
  76. long numRowsAffected();
  77. //记录下移
  78. bool next();
  79. //记录上移
  80. bool previous();
  81. //记录开头
  82. bool first();
  83. //记录结尾
  84. bool last();
  85. //是否到记录尾
  86. bool eof();
  87. //是否到记录头
  88. bool bof();
  89. private:
  90. //当前值在记录集中的问题
  91. int index;
  92. //记录集
  93. std::vector<FieldsValue> records;
  94. //驱动
  95. MySQL_Driver *m_pMysqlDriver;
  96. };
  97. #endif

接口定义:

  1. #include "stdafx.h"
  2. #include "MySQL_Driver.h"
  3. #include <assert.h>
  4. MySQL_Driver::MySQL_Driver(void)
  5. {
  6. mysql = NULL;
  7. num = 0;
  8. error="";
  9. dataSet = NULL;
  10. }
  11. MySQL_Driver::~MySQL_Driver(void)
  12. {
  13. }
  14. // 设置数据库连接参数
  15. unsigned int MySQL_Driver::connect(std::string host,std::string database, std::string user, std::string password,unsigned int port/*=3306*/)
  16. {
  17. if (connected())
  18. return 0;
  19. MYSQL *con;
  20. if(mysql == NULL)
  21. {
  22. mysql = ::mysql_init(NULL);
  23. if (mysql == NULL)
  24. {
  25. error = "初始化mysql错误";
  26. return 1;
  27. }
  28. }
  29. my_bool my_true = true;
  30. mysql_options(mysql, MYSQL_OPT_RECONNECT, &my_true);
  31. con = ::mysql_real_connect(mysql,host.c_str(),user.c_str(),password.c_str(),NULL,port,NULL,0);
  32. if(con == NULL)
  33. {
  34. error = ::mysql_error(mysql);
  35. return ::mysql_errno(mysql);
  36. }
  37. // mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "gb2312");
  38. ::mysql_set_character_set(mysql,"gb2312");
  39. unsigned int re;
  40. re = ::mysql_select_db(mysql,database.c_str());
  41. if(re != 0)
  42. {
  43. error += ::mysql_error(mysql);
  44. }
  45. return re;
  46. }
  47. //查询数据库
  48. unsigned int MySQL_Driver::exec(std::string sql)
  49. {
  50. if(dataSet != NULL )
  51. mysql_free_result(dataSet);
  52. unsigned int re;
  53. if( mysql == NULL) return 1;
  54. assert(!sql.empty());
  55. re = ::mysql_query(mysql,sql.c_str());
  56. if(re == 0)
  57. {
  58. dataSet = ::mysql_store_result(mysql);
  59. num = ::mysql_affected_rows(mysql);
  60. record.clear();
  61. records.clear();
  62. fields.clear();
  63. }
  64. else
  65. {
  66. re = ::mysql_errno(mysql);
  67. error = ::mysql_error(mysql);
  68. std::cout<<error<<std::endl;
  69. }
  70. return re;
  71. }
  72. /*获取查询得到的所有结果*/
  73. std::vector<FieldsValue> MySQL_Driver::fieldValues()
  74. {
  75. MYSQL_ROW row;
  76. unsigned int i;
  77. FieldsValue tmp;
  78. assert(mysql != NULL);
  79. if(records.size() > 0) return records;
  80. if(dataSet != NULL)
  81. {
  82. fieldsName();
  83. while(row = mysql_fetch_row(dataSet))
  84. {
  85. if(row != NULL)
  86. {
  87. for(i=0;i<fields.size();i++)
  88. {
  89. if ((char *)row[i] != NULL)
  90. {
  91. tmp[fields[i]] = (char *)row[i];
  92. }
  93. else
  94. {
  95. tmp[fields[i]] = "";
  96. }
  97. }
  98. records.push_back(tmp);
  99. }
  100. }
  101. }
  102. return records;
  103. }
  104. //返回错误信息
  105. std::string MySQL_Driver::errorCode()
  106. {
  107. return error;
  108. }
  109. //返回查询后的列值
  110. std::vector<std::string> MySQL_Driver::fieldsName()
  111. {
  112. /*
  113. field = mysql_fetch_fields(dataSet);
  114. 然后通过field[i].name访问在此有错误,不知道为什么,可能是mysql的bug
  115. */
  116. MYSQL_FIELD *field;
  117. assert(mysql != NULL);
  118. if(fields.size()>0) return fields;
  119. while(field = mysql_fetch_field(dataSet))
  120. {
  121. fields.push_back(field->name);
  122. }
  123. return fields;
  124. }
  125. //向数据库中插入一条数据
  126. unsigned int MySQL_Driver::insert(std::string table, FieldsValue *data)
  127. {
  128. FieldsValue::const_iterator iter;
  129. std::string sql;
  130. int flag=0;
  131. assert(mysql != NULL);
  132. assert(!table.empty());
  133. assert(data != NULL);
  134. for(iter = data->begin();iter!= data->end();iter++)
  135. {
  136. if(flag == 0)
  137. {
  138. sql = "insert into ";
  139. sql += table;
  140. sql += " set ";
  141. sql += iter->first;
  142. sql += "='";
  143. sql += iter->second;
  144. sql += "'";
  145. flag++;
  146. }
  147. else
  148. {
  149. sql += ",";
  150. sql += iter->first;
  151. sql += "='";
  152. sql += iter->second;
  153. sql += "'";
  154. }
  155. }
  156. return exec(sql);
  157. }
  158. //返回最后一个自动增量的值
  159. unsigned long MySQL_Driver::getlastid()
  160. {
  161. return (unsigned long)::mysql_insert_id(mysql);
  162. }
  163. //返回一条sql语句影响的行数
  164. unsigned long MySQL_Driver::numRowsAffected()
  165. {
  166. return (unsigned long)num;
  167. }
  168. //根据条件修改一条数据
  169. unsigned int MySQL_Driver::update(std::string table, FieldsValue *data, std::string condition)
  170. {
  171. FieldsValue::const_iterator iter;
  172. std::string sql;
  173. int flag=0;
  174. assert(mysql != NULL);
  175. assert(!table.empty());
  176. assert(data != NULL);
  177. for(iter = data->begin();iter!= data->end();iter++)
  178. {
  179. if(flag == 0)
  180. {
  181. sql = "update ";
  182. sql += table;
  183. sql += " set ";
  184. sql += iter->first;
  185. sql += "='";
  186. sql += iter->second;
  187. sql += "'";
  188. flag++;
  189. }
  190. else
  191. {
  192. sql += ",";
  193. sql += iter->first;
  194. sql += "='";
  195. sql += iter->second;
  196. sql += "'";
  197. }
  198. }
  199. if(!condition.empty())
  200. {
  201. sql += " where ";
  202. sql += condition;
  203. }
  204. return exec(sql);
  205. }
  206. //根据条件删除数据
  207. unsigned int MySQL_Driver::remove(std::string table, std::string condition)
  208. {
  209. std::string sql;
  210. assert(mysql != NULL);
  211. assert(!table.empty());
  212. sql = "delete from ";
  213. sql += table;
  214. if(!condition.empty())
  215. {
  216. sql += " where ";
  217. sql += condition;
  218. }
  219. return exec(sql);
  220. }
  221. bool MySQL_Driver::connected()
  222. {
  223. if (mysql == NULL || mysql->host==NULL)
  224. return false;
  225. static int lastcontime = 0;
  226. if ((GetTickCount()-lastcontime) > 60000)
  227. {
  228. if (mysql_ping(mysql)==0)
  229. {
  230. lastcontime = GetTickCount();
  231. return true;
  232. }
  233. else
  234. return false;
  235. }
  236. else
  237. {
  238. return true;
  239. }
  240. }
  241. void MySQL_Driver::disconnected()
  242. {
  243. if(dataSet != NULL)
  244. ::mysql_free_result(dataSet);
  245. fields.clear();
  246. error = "";
  247. record.clear();
  248. records.clear();
  249. ::mysql_close(mysql);
  250. mysql = NULL;
  251. }
  252. //
  253. MySQL_Connection::MySQL_Connection(MySQL_Driver* pMysqlDriver)
  254. {
  255. m_pMysqlDriver = pMysqlDriver;
  256. }
  257. MySQL_Connection::~MySQL_Connection(void)
  258. {
  259. }
  260. bool MySQL_Connection::exec(std::string sql)
  261. {
  262. if (m_pMysqlDriver != NULL)
  263. {
  264. if (m_pMysqlDriver->exec(sql)==0)
  265. {
  266. records = m_pMysqlDriver->fieldValues();
  267. index = 0;
  268. if (records.size() > 0)
  269. return true;
  270. else
  271. return false;
  272. }
  273. else
  274. return false;
  275. }
  276. return false;
  277. }
  278. bool MySQL_Connection::stringValue(std::string &str, std::string fieldName,int nFieldLen/*=-1*/)
  279. {
  280. str = records[index][fieldName];
  281. return true;
  282. }
  283. bool MySQL_Connection::intValue(long &lVal, std::string fieldName)
  284. {
  285. const char* pszVal = records[index][fieldName].c_str();
  286. lVal = atoi(pszVal);
  287. return true;
  288. }
  289. bool MySQL_Connection::floatValue(double &lVal, std::string fieldName)
  290. {
  291. const char* pszVal = records[index][fieldName].c_str();
  292. lVal = atof(pszVal);
  293. return true;
  294. }
  295. long MySQL_Connection::numRowsAffected()
  296. {
  297. if (m_pMysqlDriver != NULL)
  298. {
  299. return m_pMysqlDriver->numRowsAffected();
  300. }
  301. return 0;
  302. }
  303. bool MySQL_Connection::next()
  304. {
  305. if (m_pMysqlDriver == NULL) return false;
  306. if (++index >= (int)m_pMysqlDriver->numRowsAffected()) return false;
  307. return true;
  308. }
  309. bool MySQL_Connection::previous()
  310. {
  311. if (m_pMysqlDriver == NULL) return false;
  312. if (--index < 0) return false;
  313. return true;
  314. }
  315. bool MySQL_Connection::first()
  316. {
  317. if (m_pMysqlDriver == NULL) return false;
  318. index= 0;
  319. if (index >= (int)m_pMysqlDriver->numRowsAffected()) return false;
  320. return true;
  321. }
  322. bool MySQL_Connection::last()
  323. {
  324. if (m_pMysqlDriver == NULL) return false;
  325. index= m_pMysqlDriver->numRowsAffected() - 1;
  326. if (index >= (int)m_pMysqlDriver->numRowsAffected()) return false;
  327. if (index < 0) return false;
  328. return true;
  329. }
  330. bool MySQL_Connection::eof()
  331. {
  332. if (m_pMysqlDriver == NULL) return false;
  333. if (index == m_pMysqlDriver->numRowsAffected()) return true;
  334. return false;
  335. }
  336. bool MySQL_Connection::bof()
  337. {
  338. if (m_pMysqlDriver == NULL) return false;
  339. if (index == 0 && m_pMysqlDriver->numRowsAffected()>0) return true;
  340. return false;
  341. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/354327
推荐阅读
相关标签
  

闽ICP备14008679号