您当前的位置: 首页 > 数据库教程 > MySQL教程 > MySQL的C++封装

MySQL的C++封装

作者:不详 来源:网络 发布时间: 2014-07-17 12:20 点击:
最近的项目数据库管理系统从SQL SERVER2000迁移到了MySQL上来,之前基于ADO的连接方式连接上SQL SERVER,使用MySQL数据库管理系统之后,直接在MySQL的C语言的API上以面向对象的方式封装实现了数据库的创建,表的创建,数据库的读写操作快速搭建原型,目前没有添加连接

MySQL的C++封装

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

  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上封装实现。

  ?

  

  

  

  

  

  

  

  


  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  34

  35

  36

  37

  38

  39

  


  #ifndef __MYSQL_INTERFACE_H__

  #define __MYSQL_INTERFACE_H__

  #include "winsock.h"

  #include <iostream>

  #include <string>

  #include "mysql.h"

  #include <vector>

  #include <string>

  #pragma comment(lib, "ws2_32.lib")

  #pragma comment(lib, "libmysql.lib")

  using namespace std;

  class MySQLInterface

  {

  public:

  MySQLInterface();

  virtual ~MySQLInterface();

  bool connectMySQL(char* server, char* username, char* password, char* database,int port);

  bool createDatabase(std::string& dbname);

  bool createdbTable(const std::string& query);

  void errorIntoMySQL();

  bool writeDataToDB(string queryStr);

  bool getDatafromDB(string queryStr, std::vector<std::vector<std::string> >& data);

  void closeMySQL();

  public:

  int errorNum;              //错误代号

  const char* errorInfo;       //错误提示

  private:

  MYSQL mysqlInstance;                //MySQL对象,必备的一个数据结构

  MYSQL_RES *result;           //用于存放结果 建议用char* 数组将此结果转存

  };

  #endif

  


  ?

  

  

  

  

  

  

  

  


  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  34

  35

  36

  37

  38

  39

  40

  41

  42

  43

  44

  45

  46

  47

  48

  49

  50

  51

  52

  53

  54

  55

  56

  57

  58

  59

  60

  61

  62

  63

  64

  65

  66

  67

  68

  69

  70

  71

  72

  73

  74

  75

  76

  77

  78

  79

  80

  81

  82

  83

  84

  85

  86

  87

  88

  89

  90

  91

  92

  93

  94

  95

  96

  97

  98

  99

  100

  101

  102

  103

  104

  105

  106

  107

  108

  109

  110

  111

  112

  113

  114

  115

  116

  117

  118

  


  #include "stdafx.h"

  #include "MySQLInterface.h"

  //构造函数 初始化各个变量和数据

  MySQLInterface::MySQLInterface():

  errorNum(0),errorInfo("ok")

  {

  mysql_library_init(0,NULL,NULL);

  mysql_init(&mysqlInstance);

  mysql_options(&mysqlInstance,MYSQL_SET_CHARSET_NAME,"gbk");

  }

  MySQLInterface::~MySQLInterface()

  {

  }

  //连接MySQL

  bool MySQLInterface::connectMySQL(char* server, char* username, char* password, char* database,int port)

  {

  if(mysql_real_connect(&mysqlInstance,server,username,password,database,port,0,0) != NULL)

     return true;

  else

     errorIntoMySQL();

  return false;

  }

  //判断数据库是否存在,不存在则创建数据库,并打开

  bool MySQLInterface::createDatabase(std::string& dbname)

  {

  std::string queryStr = "create database if not exists ";

  queryStr += dbname;

  if (0 == mysql_query(&mysqlInstance,queryStr.c_str()))

  {

     queryStr = "use ";

     queryStr += dbname;

     if (0 == mysql_query(&mysqlInstance,queryStr.c_str()))

     {

         return true;

     }

     

  }

  errorIntoMySQL();

  return false;

  }

  //判断数据库中是否存在相应表,不存在则创建表

  bool MySQLInterface::createdbTable(const std::string& query)

  {

  if (0 == mysql_query(&mysqlInstance,query.c_str()))

  {

     return true;

  }

  errorIntoMySQL();

  return false;

  }

  //写入数据

  bool MySQLInterface::writeDataToDB(string queryStr)

  {

  if(0==mysql_query(&mysqlInstance,queryStr.c_str()))

     return true;

  else

     errorIntoMySQL();

  return false;

  }

  //读取数据

  bool MySQLInterface::getDatafromDB(string queryStr, std::vector<std::vector<std::string> >& data)

  {

  if(0!=mysql_query(&mysqlInstance,queryStr.c_str()))

  {

     errorIntoMySQL();

     return false;

  }

  result=mysql_store_result(&mysqlInstance);

  int row=mysql_num_rows(result);

  int field=mysql_num_fields(result);

  MYSQL_ROW line=NULL;

  line=mysql_fetch_row(result);

  int j=0;

  std::string temp;

  while(NULL!=line)

  {

     std::vector<std::string> linedata;

     for(int i=0; i<field;i++)

     {

         if(line[i])

         {

             temp = line[i];

             linedata.push_back(temp);

         }

         else

         {

             temp = "";

             linedata.push_back(temp);

         }

     }

     line=mysql_fetch_row(result);

     data.push_back(linedata);

  }

  return true;

  }

  //错误信息

  void MySQLInterface::errorIntoMySQL()

  {

  errorNum=mysql_errno(&mysqlInstance);

  errorInfo=mysql_error(&mysqlInstance);

  }

  //断开连接

  void MySQLInterface::closeMySQL()

  {

  mysql_close(&mysqlInstance);

  }

  
分享到:
本文"MySQL的C++封装"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4