c++ 封装mysql mysqlinterface_Mysql API之C++封装(一)

#ifndef __DB_CLIENT_H__

#define __DB_CLIENT_H__

#include

#include

#include

/**

* @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[MAX_CHAR_LEN];

/*db port*/

unsigned short m_usDBPort;

/*db name*/

char m_szDBName[MAX_CHAR_LEN];

/*db username*/

char m_szUserName[MAX_CHAR_LEN];

/*db user's password*/

char m_szPassword[MAX_CHAR_LEN];

/*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

#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

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-02-14 15:51

浏览 1840

分类:数据库

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值