2014-07-13 15:47:21
来 源
中存储网
mysqlcmd
自己封装的一个mysql访问。分为3个类:1.数据库连接类2.sql语句执行类3.查询语句结果访问。功能不是非常健全,也可能有一些问题。--------------------------------------------------------------
自己封装的一个mysql访问。

分为3个类:

1.数据库连接类

2.sql语句执行类

3.查询语句结果访问。

功能不是非常健全,也可能有一些问题。

--------------------------------------------------------------------------------------------------

#pragma once

#include <Windows.h>

#include <mysql.h>

#include <string>

#include <vector>

#include <iostream>

using namespace std;

// 连接服务器时的返回消息

#define MYSQL_MSG_EMPTY 0

#define MYSQL_CONNECT_ERROR -1

#define MYSQL_CONNECT_SUCCESS 1

// 获取查询语句的需要返回的数据类型

class SelectResult;

struct dataPos;

class CDBConnector

{

public:

CDBConnector();

~CDBConnector();

public:

int Connect(string hostAddress, string userName, string password, string dbName, int port = 3306); //创建数据库连接

bool SelectDataBase(string dbName); //选择数据库

void Close(); //关闭数据库连接

MYSQL* GetMysqlObject(); //返回MYSQL*

private:

MYSQL m_mysql;

};

class CDBCommand

{

public:

CDBCommand(MYSQL* mysql);

~CDBCommand();

public:

bool Command(string sql); //执行sql语句

SelectResult* GetResult(); //获取查询结果

void Clear();

private:

void FreeDataList();

private:

MYSQL* m_ptMysql;

MYSQL_RES* m_ptResult;

vector<SelectResult*> m_resultList;

};

class SelectResult

{

friend class CDBCommand;

public:

SelectResult();

SelectResult(vector<vector<string>> tempList);

~SelectResult();

int GetRowCount(); //返回查询结果的行数

int GetColCount(); //返回查询结果的列数

string GetDataAt(int row, int col); //取某一位置的数据

private:

void SetRowCount(int rowCount);

void SetColCount(int colCount);

private:

vector<vector<string>> m_dataList;

int m_numRow;

int m_numCol;

};

-------------------------------------------------------------------------------------------

#include "TestService.h"

CDBConnector::CDBConnector()

{

mysql_init(&m_mysql);

}

CDBConnector::~CDBConnector()

{

Close();

}

int CDBConnector::Connect(string hostAddress, string userName, string password, string dbName, int port)

{

if (hostAddress.empty() || userName.empty() || password.empty() || dbName.empty())

{

return MYSQL_MSG_EMPTY;

}

if (mysql_real_connect(&m_mysql, hostAddress.c_str(), userName.c_str(), password.c_str(), dbName.c_str(),

port, NULL, 0))

{

mysql_query(&m_mysql, "SET NAMES GBK"); //设置编码格式,否则在cmd下无法显示中文

return MYSQL_CONNECT_SUCCESS;

}

return MYSQL_CONNECT_ERROR;

}

bool CDBConnector::SelectDataBase(string dbName)

{

if (mysql_select_db(&m_mysql, dbName.c_str()) != 0)

{

return false;

}

return true;

}

void CDBConnector::Close()

{

mysql_close(&m_mysql);

}

MYSQL* CDBConnector::GetMysqlObject()

{

return &m_mysql;

}

CDBCommand::CDBCommand(MYSQL* mysql):m_ptMysql(mysql)

{

}

CDBCommand::~CDBCommand()

{

Clear();

}

void CDBCommand::FreeDataList()

{

int count = m_resultList.size();

for (int i = count - 1; i >= 0; i--)

{

delete m_resultList[i];

m_resultList.clear();

}

}

bool CDBCommand::Command(string sql)

{

if (mysql_query(m_ptMysql, sql.c_str()) != 0)

{

return false;

}

else

{

return true;

}

}

SelectResult* CDBCommand::GetResult()

{

MYSQL_ROW row;

MYSQL_RES* res_set;

res_set = mysql_store_result(m_ptMysql);

if (res_set == NULL)

{

return NULL;

}

vector<vector<string>> dataList;

int rowCount = 0;

while ((row = mysql_fetch_row(res_set)) != NULL)

{

vector<string> list;

for (size_t i = 0; i < mysql_num_fields(res_set); i++)

{

list.push_back(row[i]);

}

dataList.push_back(list);

++rowCount;

}

SelectResult* result = new SelectResult(dataList);

result->SetColCount(mysql_num_fields(res_set));

result->SetRowCount(rowCount);

m_resultList.push_back(result);

mysql_free_result(res_set);

return result;

}

void CDBCommand::Clear()

{

FreeDataList();

}

SelectResult::SelectResult()

{

}

SelectResult::SelectResult(vector<vector<string>> tempList)

{

for (vector<vector<string>>::iterator it = tempList.begin(); it != tempList.end(); it++)

{

vector<string> temp(it->size());

std::copy(it->begin(), it->end(), temp.begin());

m_dataList.push_back(temp);

}

}

SelectResult::~SelectResult()

{

}

string SelectResult::GetDataAt(int row, int col)

{

if (row > m_numRow || row < 0 || col > m_numCol || col < 0)

{

return "#Error#";

}

else

{

return m_dataList[row][col];

}

}

int SelectResult::GetRowCount()

{

return m_numRow;

}

int SelectResult::GetColCount()

{

return m_numCol;

}

void SelectResult::SetRowCount(int rowCount)

{

m_numRow = rowCount;

}

void SelectResult::SetColCount(int colCount)

{

m_numCol = colCount;

}

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。