MySQL的C++封装

来源:互联网 发布:mac 不能更新10.11.6 编辑:程序博客网 时间:2024/05/18 13:09

最近的项目数据库管理系统从SQL SERVER2000迁移到了MySQL上来,之前基于ADO的连接方式连接上SQL SERVER,使用MySQL数据库管理系统之后,直接在MySQL的C语言的API上以面向对象的方式封装实现了数据库的创建,表的创建,数据库的读写操作快速搭建原型,目前没有添加连接池模块和事务处理。


源码托管在github上:https://github.com/figot/MySQLWrapper

1.MySQL的特性

使用C和C++编写,并使用了多种编译器进行测试,保证源代码的可移植性。
支持AIX、BSDi、FreeBSD、HP-UX、Linux、Mac OS、Novell NetWare、NetBSD、OpenBSD、OS/2 Wrap、Solaris、Windows等多种操作系统。
为多种編程语言提供了API。这些編程语言包括C、C++、C#、VB.NET、Delphi、Eiffel、Java、Perl、PHP、Python、Ruby和Tcl等。
支持多線程,充分利用CPU资源,支持多用户。
優化的SQL查询算法,有效地提高查询速度。
既能够作为一个单独的应用程序在客户端服务器网络环境中运行,也能够作为一个程序库而嵌入到其他的软件中。
提供多语言支持,常见的编码如中文的GB 2312、BIG5,日文的Shift JIS等都可以用作數據表名和數據列名。
提供TCP/IP、ODBC和JDBC等多种数据库连接途径。
提供用于管理、检查、优化数据库操作的管理工具。
可以处理拥有上千万条记录的大型数据库。

2.C++的API封装

用C++连接SQL有两种可直接使用的接口:MySQL Connector/C++和MySQL+ +,<1>MySQL Connector/C++是最新发布的MySQL连接器,由Sun Microsystems开发。MySQL connector为C++提供面向对象的编程接口(API)和连接MySQL Server的数据库驱动器与现存的driver不同,Connector/C++是JDBC API在C++中的实现。换句话说,Connector/C++ driver的接口主要是基于Java语言的JDBC API。Java数据库连接(JDBC)是Java连接各种数据库的业界标准。Connector/C++实现了JDBC 4.0的大部分规范。熟悉JDBC编程的C++程序开发者可以提高程序开发的效率。

<2>MySQL++是一个用C++封装了MySQL的C API的类库。它是建立标准C ++标准库(STL)之上,使处理数据库处理STL容器一样容易。此外,MySQL的++提供了让你避免最重复的工作,提供了原生C++接口。

3.MySQL的C++封装实现

在快速搭建原型的过程中,没有用到这两种连接方式,直接在MySQL C API上封装实现。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ifndef __MYSQL_INTERFACE_H__  
  2. #define __MYSQL_INTERFACE_H__  
  3.   
  4. #include "winsock.h"  
  5. #include <iostream>  
  6. #include <string>  
  7. #include "mysql.h"  
  8. #include <vector>  
  9. #include <string>  
  10.   
  11. #pragma comment(lib, "ws2_32.lib")  
  12. #pragma comment(lib, "libmysql.lib")  
  13. using namespace std;  
  14.   
  15. class MySQLInterface  
  16. {  
  17. public:    
  18.     MySQLInterface();  
  19.     virtual ~MySQLInterface();  
  20.   
  21.     bool connectMySQL(char* server, char* username, char* password, char* database,int port);  
  22.     bool createDatabase(std::string& dbname);  
  23.     bool createdbTable(const std::string& query);  
  24.   
  25.     void errorIntoMySQL();  
  26.     bool writeDataToDB(string queryStr);  
  27.     bool getDatafromDB(string queryStr, std::vector<std::vector<std::string> >& data);  
  28.     void closeMySQL();  
  29.   
  30. public:  
  31.     int errorNum;                    //错误代号  
  32.     const char* errorInfo;             //错误提示  
  33.   
  34. private:  
  35.     MYSQL mysqlInstance;                      //MySQL对象,必备的一个数据结构  
  36.     MYSQL_RES *result;                 //用于存放结果 建议用char* 数组将此结果转存  
  37. };  
  38.   
  39. #endif  


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include "MySQLInterface.h"  
  3.   
  4.   
  5. //构造函数 初始化各个变量和数据  
  6. MySQLInterface::MySQLInterface():  
  7.     errorNum(0),errorInfo("ok")  
  8. {  
  9.     mysql_library_init(0,NULL,NULL);  
  10.     mysql_init(&mysqlInstance);  
  11.     mysql_options(&mysqlInstance,MYSQL_SET_CHARSET_NAME,"gbk");  
  12. }  
  13.   
  14. MySQLInterface::~MySQLInterface()  
  15. {  
  16.   
  17. }  
  18.   
  19. //连接MySQL  
  20. bool MySQLInterface::connectMySQL(char* server, char* username, char* password, char* database,int port)  
  21. {  
  22.     if(mysql_real_connect(&mysqlInstance,server,username,password,database,port,0,0) != NULL)  
  23.         return true;  
  24.     else  
  25.         errorIntoMySQL();  
  26.     return false;  
  27. }  
  28. //判断数据库是否存在,不存在则创建数据库,并打开  
  29. bool MySQLInterface::createDatabase(std::string& dbname)  
  30. {  
  31.     std::string queryStr = "create database if not exists ";  
  32.     queryStr += dbname;  
  33.     if (0 == mysql_query(&mysqlInstance,queryStr.c_str()))  
  34.     {  
  35.         queryStr = "use ";  
  36.         queryStr += dbname;  
  37.         if (0 == mysql_query(&mysqlInstance,queryStr.c_str()))  
  38.         {  
  39.             return true;  
  40.         }  
  41.           
  42.     }  
  43.     errorIntoMySQL();  
  44.     return false;  
  45. }  
  46. //判断数据库中是否存在相应表,不存在则创建表  
  47. bool MySQLInterface::createdbTable(const std::string& query)  
  48. {  
  49.     if (0 == mysql_query(&mysqlInstance,query.c_str()))  
  50.     {  
  51.         return true;  
  52.     }  
  53.     errorIntoMySQL();  
  54.     return false;  
  55. }  
  56.   
  57. //写入数据  
  58. bool MySQLInterface::writeDataToDB(string queryStr)  
  59. {  
  60.     if(0==mysql_query(&mysqlInstance,queryStr.c_str()))  
  61.         return true;  
  62.     else  
  63.         errorIntoMySQL();  
  64.     return false;     
  65. }  
  66. //读取数据  
  67. bool MySQLInterface::getDatafromDB(string queryStr, std::vector<std::vector<std::string> >& data)  
  68. {  
  69.     if(0!=mysql_query(&mysqlInstance,queryStr.c_str()))  
  70.     {  
  71.         errorIntoMySQL();  
  72.         return false;  
  73.     }  
  74.   
  75.     result=mysql_store_result(&mysqlInstance);  
  76.   
  77.     int row=mysql_num_rows(result);  
  78.     int field=mysql_num_fields(result);  
  79.   
  80.     MYSQL_ROW line=NULL;  
  81.     line=mysql_fetch_row(result);  
  82.   
  83.     int j=0;  
  84.     std::string temp;  
  85.     while(NULL!=line)  
  86.     {     
  87.         std::vector<std::string> linedata;  
  88.         for(int i=0; i<field;i++)  
  89.         {  
  90.             if(line[i])  
  91.             {  
  92.                 temp = line[i];  
  93.                 linedata.push_back(temp);  
  94.             }  
  95.             else  
  96.             {  
  97.                 temp = "";  
  98.                 linedata.push_back(temp);  
  99.             }  
  100.         }  
  101.         line=mysql_fetch_row(result);  
  102.         data.push_back(linedata);  
  103.     }  
  104.     return true;  
  105. }  
  106.   
  107. //错误信息  
  108. void MySQLInterface::errorIntoMySQL()  
  109. {  
  110.     errorNum=mysql_errno(&mysqlInstance);  
  111.     errorInfo=mysql_error(&mysqlInstance);  
  112. }  
  113.   
  114. //断开连接  
  115. void MySQLInterface::closeMySQL()  
  116. {  
  117.     mysql_close(&mysqlInstance);  
  118. }  


FROM:  http://blog.csdn.net/figo829/article/details/22328693

0 0
原创粉丝点击