Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46582
  • 博文数量: 5
  • 博客积分: 35
  • 博客等级: 民兵
  • 技术积分: 105
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-16 17:40
文章分类

全部博文(5)

文章存档

2017年(1)

2016年(1)

2015年(2)

2013年(1)

我的朋友

分类: C/C++

2013-03-13 09:02:50

    准备要自己做点小项目了,一直没有用过关系型数据库,这次选用的是Mysql,大致看了一下甲骨文提供的API,能够领会其中的意思,但是感觉总是写API过于繁琐,因此写个类吧,这样能好一点。以下贴出代码是我写的关于mysql操作的类,不足之处,请指正,谢谢哈。

一、mysqlapi.h主要定义了错误代码

点击(此处)折叠或打开

  1. /*
  2.  * mysqlapi.h
  3.  *
  4.  * Created on: 2013-3-11
  5.  * Author: er_guangqiang
  6.  */

  7. #ifndef MYSQLAPI_H_
  8. #define MYSQLAPI_H_
  9. //Errno number
  10. #define MO_OK    0x00000000    //操作成功
  11. #define HOST_TOO_LONG    0xFFFF0001 //主机名过长
  12. #define USERNAME_TOO_LONG    0xFFFF0002 //数据库用户名过长
  13. #define PASSWD_TOO_LONG    0xFFFF0003    //数据库密码过长
  14. #define DATABASENAME_TOO_LONG    0xFFFF0004 //数据库名字过长
  15. #define PORT_SET_ERR    0xFFFF0005    //端口设置错误
  16. #define MYSQL_INIT_ERR    0xFFFF0007    //Mysql初始化失败
  17. #define MYSQL_CONNECT_ERR    0xFFFF0008    //数据库连接失败
  18. #define MYSQL_STOR_RESULTS_ERR    0xFFFF0009    //储存数据库结果失败
  19. #define MYSQL_FIELDS_ZERO    0xFFFF000a    //获取的字段列为0
  20. #define MYSQ_FETCH_FIELDS_ERR    0xFFFF000b    //获取字段失败
  21. #define HOST_NULL 0xFFFF000c    //主机名为空
  22. #define USERNAME_NULL    0xFFFF000d    //用户名为空
  23. #define PASSWD_NULL    0xFFFF000e    //数据库密码为空
  24. #define MYSQL_SWAP_OUT    0xFFFF000f    //mysql_fetch_row结果集已取空
  25. #define RESULT_NULL    0xFFFF0010    //结果指针为空,请确认是否执行了获取结果集方法
  26. #endif /* MYSQLAPI_H_ */
二、MysqlOperate.h定义了MysqlOperate类的成员变量和成员函数

点击(此处)折叠或打开

  1. /*
  2.  * MysqlOperate.h
  3.  *
  4.  * Created on: 2013-3-9
  5.  * Author: er_guangqiang
  6.  */

  7. #ifndef MYSQLOPERATE_H_
  8. #define MYSQLOPERATE_H_

  9. #include <string.h>
  10. #include <iostream>
  11. #include <mysql.h>
  12. #include "mysqlapi.h"


  13. class MysqlOperate {
  14. public:
  15.     MysqlOperate();
  16.     MysqlOperate(const char *host, const char *user, const char *passwd, const char *db, \
  17.             unsigned int port, unsigned long clientflag);
  18.     int InitMysql(const char *host, const char *user, const char *passwd, const char *db, \
  19.             unsigned int port, unsigned long clientflag);
  20.     int ConnectMysql();
  21.     int ExcuteQuery(const char *sqlquery);
  22.     int FetchResult();
  23.     unsigned int FetchRowtoBuffer(char **buffer);
  24.     unsigned long GetRowsCount();
  25.     unsigned int GetFieldsCount();
  26.     int FetchColumnName(char **column);
  27.     void ReleaseConnMysql();
  28.     virtual ~MysqlOperate();
  29. private:
  30.     MYSQL *m_mysql;
  31.     MYSQL_RES * m_results;
  32.     MYSQL_ROW m_record;
  33.     MYSQL_FIELD *m_fields;
  34.     char m_host[16];
  35.     char m_user[16];
  36.     char m_passwd[32];
  37.     char m_db[16];
  38.     int m_port;
  39.     long m_clientflag;
  40. };

  41. #endif /* MYSQLOPERATE_H_ */
三、MysqlOperate.cpp主要实现头文件中定义的成员函数


点击(此处)折叠或打开

  1. /*
  2.  * MysqlOperate.cpp
  3.  *
  4.  * Created on: 2013-3-9
  5.  * Author: er_guangqiang
  6.  */

  7. #include "MysqlOperate.h"

  8. //构造函数,主要任务复位成员变量
  9. MysqlOperate::MysqlOperate() {
  10.     // TODO Auto-generated constructor stub
  11.     memset(m_host, 0x00, 16);
  12.     memset(m_user, 0x00, 16);
  13.     memset(m_passwd, 0x00, 32);
  14.     memset(m_db, 0x00, 16);
  15.     m_port = 3306;
  16.     m_clientflag = 0;
  17.     m_mysql = NULL;
  18.     m_results = NULL;
  19. }

  20. /*重载构造函数
  21.  *输入: host:主机ip
  22.  *        user:mysql用户名
  23.  *        passwd:mysql密码
  24.  *        db:选择mysql中的数据库
  25.  *        port:设置mysql端口
  26.  *        clientflag:mysql客户端参数
  27.  *输出:
  28.  *        无
  29.  *    次函数主要负责初始化Mysql的信息
  30. */
  31. MysqlOperate::MysqlOperate(const char *host, const char *user, const char *passwd, const char *db, \
  32.             unsigned int port, unsigned long clientflag)
  33. {
  34.     int r = 0;
  35.     memset(m_host, 0x00, 16);
  36.     memset(m_user, 0x00, 16);
  37.     memset(m_passwd, 0x00, 32);
  38.     memset(m_db, 0x00, 16);
  39.     m_port = 3306;
  40.     m_clientflag = 0;
  41.     r = InitMysql(host,user,passwd,db,port,clientflag);
  42.     if(0 == r)
  43.         std::cout<< "InitMysql Success!!!"<<std::endl;
  44.     else
  45.         std::cout << "Errno = " << r << std::endl;

  46.     m_mysql = NULL;
  47.     m_results = NULL;
  48. }
  49. //初始化mysql信息
  50. /*
  51.  * 输入:与MysqlOperate()构造函数相同
  52.  */
  53. int MysqlOperate::InitMysql(const char *host, const char *user, const char *passwd, const char *db, \
  54.             unsigned int port, unsigned long clientflag)
  55. {
  56.     if(NULL == host)
  57.         return HOST_NULL;
  58.     else if(strlen(host) > 16)
  59.         return HOST_TOO_LONG;
  60.     else if (strlen(host) >= 0 && strlen(host) <= 16)
  61.         strncpy(m_host, host, strlen(host));

  62.     if(NULL == user)
  63.         return USERNAME_NULL;
  64.     else if(strlen(user) > 16)
  65.         return     USERNAME_TOO_LONG;
  66.     else if(strlen(user) >= 0 && strlen(user) <= 16)
  67.         strncpy(m_user, user, strlen(user));

  68.     if(NULL == passwd)
  69.         return PASSWD_NULL;
  70.     else if(strlen(passwd) > 32)
  71.         return PASSWD_TOO_LONG;
  72.     else if(strlen(passwd) >= 0 && strlen(passwd) <= 32)
  73.         strncpy(m_passwd, passwd, strlen(passwd));

  74.     if(NULL == db)
  75.     {
  76.         std::cout << "Warning!!!---database set null---! Will you create database??"<< std::endl;
  77.         return MO_OK;
  78.     }
  79.     else if(strlen(db) > 16)
  80.         return DATABASENAME_TOO_LONG;
  81.     else
  82.         strncpy(m_db, db, strlen(db));
  83.     if(port <= 0)
  84.         return PORT_SET_ERR;
  85.     else
  86.         m_port = port;
  87.     m_clientflag = clientflag;
  88.     return MO_OK;
  89. }

  90. //连接Mysql数据库
  91. /*
  92.  * 输入:
  93.  *         无
  94.  * 输出:
  95.  *         无
  96.  * 此函数旨在连接Mysql数据库
  97.  */
  98. int MysqlOperate::ConnectMysql()
  99. {
  100.     m_mysql = mysql_init(NULL);
  101.     if(NULL == m_mysql)
  102.     {
  103.         std::cout << "mysql_init failed" << std::endl;
  104.         return MYSQL_INIT_ERR;
  105.     }
  106.     else
  107.         std::cout << "mysql_init success" << std::endl;
  108.     mysql_real_connect(m_mysql, m_host, m_user, m_passwd, m_db, m_port, NULL, m_clientflag);
  109.     if(NULL == m_mysql)
  110.     {
  111.         std::cout << "mysql_real_connect failed" << std::endl;
  112.         return MYSQL_CONNECT_ERR;
  113.     }
  114.     else
  115.         std::cout << "mysql_real_connect success" << std::endl;
  116.     return MO_OK;
  117. }

  118. //执行sql语句
  119. /*
  120.  * 输入:
  121.  *        sqlquery:sql语句字符串
  122.  * 输出:
  123.  *         无
  124.  * 此函数为执行定义的sql语句
  125.  */
  126. int MysqlOperate::ExcuteQuery(const char *sqlquery)
  127. {
  128.     return mysql_query(m_mysql, sqlquery);
  129. }

  130. int MysqlOperate::FetchResult()
  131. {
  132.     m_results = mysql_store_result(m_mysql);
  133.     if(NULL == m_results)
  134.     {
  135.         std::cout << "mysql_store_result failed" << std::endl;
  136.         return MYSQL_STOR_RESULTS_ERR;
  137.     }
  138.     else
  139.         std::cout << "mysql_store_result success" << std::endl;
  140.     return MO_OK;
  141. }

  142. //获取行内容(一行)
  143. /*
  144.  * 输入/输出:
  145.  *         buffer:行内容存储容器
  146.  * 此函数为获取结果集的一行内容(在此之前一定要先执行sql语句,保证m_results不为空)
  147.  */
  148. unsigned int MysqlOperate::FetchRowtoBuffer(char **buffer)
  149. {
  150.     if(NULL == m_results)
  151.         return RESULT_NULL;
  152.     if((m_record = mysql_fetch_row(m_results)))
  153.     {
  154.         int c = GetFieldsCount();
  155.         int i = 0;
  156.         while(i < c)
  157.         {
  158.             strncpy(*(buffer + i), m_record[i], strlen(m_record[i]));
  159.             i++;
  160.         }
  161.         return MO_OK;
  162.     }
  163.     return MYSQL_SWAP_OUT;

  164. }

  165. //获取字段个数
  166. /*
  167.  * 输入/输出:
  168.  *         无
  169.  * 此函数为获取结果集的字段个数
  170.  */
  171. unsigned int MysqlOperate::GetFieldsCount()
  172. {
  173.     if(NULL == m_results)
  174.     {
  175.         std::cout << "m_results is NULL, please confirm mysql_store_result success" << std::endl;
  176.         return MO_OK;
  177.     }
  178.     return mysql_num_fields(m_results);
  179. }

  180. //获取行数
  181. /*
  182.  * 输入/输出:
  183.  *         无
  184.  * 此函数为获取结果集的行数
  185.  */
  186. unsigned long MysqlOperate::GetRowsCount()
  187. {
  188.     if(NULL == m_results)
  189.     {
  190.         std::cout << "m_results is NULL, please confirm mysql_store_result success" << std::endl;
  191.         return MO_OK;
  192.     }
  193.     return (unsigned long)mysql_num_rows(m_results);
  194. }

  195. //获取字段内容
  196. /*
  197.  * 输入/输出:
  198.  *         column:字段内容容器
  199.  * 此函数为获取结果集的字段内容
  200.  */
  201. int MysqlOperate::FetchColumnName(char **column)
  202. {
  203.     unsigned int c = GetFieldsCount();
  204.     if(0 == c)
  205.     {
  206.         std::cout << "GetFieldsCount is zero." << std::endl;
  207.         return MYSQL_FIELDS_ZERO;
  208.     }
  209.     m_fields = mysql_fetch_field(m_results);
  210.     if(NULL == m_fields)
  211.     {
  212.         std::cout << "mysql_fetch_field is failed" << std::endl;
  213.         return MYSQ_FETCH_FIELDS_ERR;
  214.     }
  215.     else
  216.         std::cout << "mysql_fetch_field is success" <<std::endl;
  217.     unsigned int i = 0;
  218.     for(; i < c; i++)
  219.         strncpy(column[i], m_fields[i].name, m_fields[i].name_length);
  220.     return MO_OK;
  221. }

  222. //释放连接
  223. /*
  224.  * 输入/输出:
  225.  *         无
  226.  * 此函数为释放Mysql连接
  227. */
  228. void MysqlOperate::ReleaseConnMysql()
  229. {
  230.     if(NULL != m_results)
  231.         mysql_free_result(m_results);
  232.     if(NULL != m_mysql)
  233.         mysql_close(m_mysql);
  234. }

  235. MysqlOperate::~MysqlOperate() {
  236.     // TODO Auto-generated destructor stub
  237. }

以上就是我的代码,里面有函数的输入输出了功能简介,再次不赘述,我是新人,指教哈


阅读(2384) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~