Mysql API之C++封装(一)
由于项目要求原因,需要用C或者C++调用mysql的API接口来操作mysql数据库,在google和百度上搜了一下,发现两个比较mini的接口类,感觉比较不错,下载下来编译了一下,改了一些错误,源码如下:DBClient.h ------
#ifndef __DB_CLIENT_H__#define __DB_CLIENT_H__#include <mysql.h>#include <stdlib.h>#include <string.h>/*** @brief enumaration for sql cmd type*/enum Cmd{CMD_SELECT,CMD_INSERT,CMD_DELETE,CMD_UPDATE};#define MAX_CHAR_LEN 64/*** @brief this is a class for operating the mysql database*/class DBClient{public:DBClient(): m_usDBPort(0), m_pResultSet(NULL){memset(m_szDBIP, 0, sizeof(m_szDBIP));memset(m_szUserName, 0, sizeof(m_szUserName));memset(m_szPassword, 0, sizeof(m_szPassword));memset(m_szDBName, 0, sizeof(m_szDBName));}~DBClient(){FreeResultSet();mysql_close(&m_oDBHandle);}/*** @brief initialize the db connection info* @param pDBIP, the db ip address* @param usDBPort, the db port* @param pDBName, the db name* @param pUserName, the db username* @param pPassword, the db password* @return true if init success, otherwise, false*/bool Init(const char * pDBIP, unsigned short usDBPort,const char * pDBName, const char * pUserName, const char * pPassword);/*** @brief connect the db server* @return true if connect success, otherwise, false*/bool Connect();/*** @brief execute the sql statement* @param pSqlStr, the sql statement pointer* @return true if execute success, otherwise, false*/bool Execute(const char * pSqlStr, Cmd eCmd);/*** @brief get the next result of the operation* @param pRes, the result row* @param pLen, the array of the length of every fields* @param nNum, the num of fields* @return true if get the next result success, otherwise, false*/bool GetNextResult(char **& pRes, unsigned long *& pLen, int & nNum);/*** @brief get the most recent error No.*/unsigned int GetErrorNo(){return mysql_errno(&m_oDBHandle);}private:/*** @brief free the memory of the result set*/void FreeResultSet();private:/*db ip address*/char m_szDBIP;/*db port*/unsigned short m_usDBPort;/*db name*/char m_szDBName;/*db username*/char m_szUserName;/*db user's password*/char m_szPassword;/*the db connection handle object*/MYSQL m_oDBHandle;/*the pointer of result set for operating's result*/MYSQL_RES * m_pResultSet;};#endif // __DB_CLIENT_H__
DBClient.cpp实现如下:
#include "DBClient.h"#include <string.h>#pragma comment(lib, "libmysql.lib")bool DBClient::Init(const char * pDBIP, unsigned short usDBPort,const char * pDBName, const char * pUserName, const char * pPassword){strcpy(m_szDBIP, pDBIP);m_usDBPort = usDBPort;strcpy(m_szDBName, pDBName);strcpy(m_szUserName, pUserName);strcpy(m_szPassword, pPassword);if (NULL == mysql_init(&m_oDBHandle)){return false;}return true;}bool DBClient::Connect(){if (NULL == mysql_real_connect(&m_oDBHandle, m_szDBIP,m_szUserName, m_szPassword,m_szDBName, m_usDBPort,NULL, 0)){return false;}return true;}bool DBClient::Execute(const char * pSqlStr, Cmd eCmd){if (0 != mysql_query(&m_oDBHandle, pSqlStr)){return false;}if (CMD_SELECT == eCmd){FreeResultSet();if (NULL == (m_pResultSet = mysql_store_result(&m_oDBHandle))){return false;}}return true;}bool DBClient::GetNextResult(char **& pRes, unsigned long *& pLen,int & nNum){MYSQL_ROW pRow = mysql_fetch_row(m_pResultSet);if(NULL == pRow){return false;}pRes = pRow;nNum = mysql_num_fields(m_pResultSet);pLen = mysql_fetch_lengths(m_pResultSet);return true;}void DBClient::FreeResultSet(){if (NULL != m_pResultSet){mysql_free_result(m_pResultSet);}m_pResultSet = NULL;}
接口比较简洁明快,如果你感觉功能比较简单的话,可以下载开源的SQLAPI++,我这有拷贝,需要的可以给我的邮箱发邮件,我的邮箱lppchina@gmail.com
页:
[1]