您的位置:首页 > 数据库 > MySQL

mysql的常用操作的封装

2013-08-18 23:24 330 查看
1、概述:

为了把繁琐的操作简化成简单的类,设计了2个类用来封装了mysql的常用操作,以便使用者可以方便地使用。

2、组成

1)数据库操作类CDatabaseConnect

2)SQL对象类CSqlStatement

3、类的头文件代码

#include <winsock.h>

#include "mysql.h"

#include <string>

using namespace std;

//MYSQL的返回码类型

#define MYSQL_OK 0

#define MYSQL_FALSE !(MYSQL_OK)

#define MYSQL_ERROR 1

//SQL的SQL语句类型

#define MYSQL_SQL_TYPE_INSERT 0

#define MYSQL_SQL_TYPE_QUERY 1

//字符串的大小

#define MYSQL_CHAR_SIZE 0xFF+1

#define MYSQL_STRING_SIZE 0xFF+1

#define MYSQL_VARCHAR_SIZE 0xFFFF+1

#define MYSQL_VAR_STRING_SIZE 0xFFFF+1

#define MYSQL_MEDIUMBLOB_SIZE 0xFFFFFF+1

#define MYSQL_LONGBLOB_SIZE 0xFFFFFFFF+1

//日志入口函数指针

typedef void(*PAddLogFunc)(const char *pszData);

//数据库连接参数

typedef struct tagSTConnectParam

{
char szUser[64+1];//用户名称
char szPasswd[64+1];//用户密码
char szHost[64+1];//主机名称
char szDbName[64+1];//数据库名称
char *pszSocket;//socket
unsigned int nPort;//端口号
unsigned long ucClientFlag;//客户端标识

}STConnectParam, *PSTConnectParam;

//SQL预处理对象

class CSqlStatement

{

public:
CSqlStatement();
CSqlStatement(MYSQL* pDb);
virtual ~CSqlStatement();

private:
friend class CDatabaseConnect;
CSqlStatement(MYSQL* pDb, const char *pszSqlStatement);

public:

    //行数
int GetRowCount(void);

    //列数
int GetColumnCount(void);
//nPosIndex started by zero
//列名称
const char *GetColumnName(int nPosIndex);

    //列ID
int GetColumnIndex(const char *pszColumnName);
//获取整型
int GetColumnValueInt(int nPosIndex);

    //获取短字符串
const char *GetColumnValueVarChar(int nPosIndex);

    //获取长字符串
const char *GetColumnValueString(int nPosIndex);

    //获取字符串
const char *GetColumnValueBlob(int nPosIndex, int *pnLen);

    //获取浮点型
double GetColumnValueDouble(int nPosIndex);

    //绑定整型
bool BindInt(int nPosIndex, const int &nValue);

    //绑定短字符串
bool BindChar(int nPosIndex, const char *pszValue);

    //绑定长字符串
bool BindString(int nPosIndex, const char *pszValue);

    //绑定字符串
bool BindBlob(int nPosIndex, const char *pszValue, int nBlobBytes);
//获取浮点型
bool BindDouble(int nPosIndex, const double &fValue);
//运行
bool Execute(void);

    //下一行
bool NextRow(void);

    //复位
bool Reset(void);

    //写日志
int WriteDbLog(const char *pszMsg);

    //设置SQL预处理对象
int SetSqlStatement(const char *pszSqlStatement);

private:

    //初始化数据
bool InitData(void);

    //释放数据
void FreeData(void);

private:

    //日志函数接口
PAddLogFunc pAddLog;
MYSQL_STMT *m_pStmt;
MYSQL* m_pDb;
MYSQL_RES *m_pPrepareMetaResult;
MYSQL_BIND *m_pBind;
unsigned long *m_plStringLen;
my_bool *m_pbArray;
int *m_pnDataArray;
double *m_pfDataArray;
char **m_pszDataArray;
string m_strSql;
string m_strErrMsg;
int m_nRowCount;
int m_nColumnCount;
int m_nSqlType;
int m_nBindParamFlag;

};

typedef CSqlStatement *PCSqlStatement;

//数据库连接

class CDatabaseConnect

{

public:
CDatabaseConnect();
virtual ~CDatabaseConnect();
//打开
bool Open(PSTConnectParam pstParam);

        //关闭
bool Close(void);

        //获取MySQL对象指针
MYSQL *GetMySQL(void);

        //获取指定表名称的记录个数
int  GetTableRecordCount(const char *pszTabelName);

        //写日志
int WriteDbLog(const char *pszMsg);

        //运行SQL语句
bool DirectStatement(const char *pszSqlStatement);

        //产生SQL预处理对象
CSqlStatement* SetSqlStatement(const char *pszSqlStatement);

        //释放SQL对象
void ReleaseStatement(void);

        //获取指定SQL语句的查询记录个数
int GetRecordCount(const char *szSql);
//事务
bool BeginTransaction(void);
bool CommitTransaction(void);
bool RollbackTransaction(void);

public:
MYSQL * m_pDb;

private:
PAddLogFunc pAddLog;
CSqlStatement *m_pSqlStatement;
string m_strDbName;
string m_strSql;
string m_strErrMsg;

};

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ MySQL