MySQL的C++封裝

來源:互聯網
上載者:User

   最近的項目資料庫管理系統從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); }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.