当前位置: 技术问答>linux和unix
本人对MYSQL C API做的一个封装,希望对linux C++程序员有点帮助,同时欢迎拍砖!
来源: 互联网 发布时间:2016-02-26
本文导语: 本人对MYSQL C API做的一个封装,使用很简单,构造一个对象,就可直接执行SQL,但对于需返回结果的查询语句,对其数据集没做封装,仍需直接调用mysql c api对其进行处理,本人将下次完成,不管如何,欢迎大家...
本人对MYSQL C API做的一个封装,使用很简单,构造一个对象,就可直接执行SQL,但对于需返回结果的查询语句,对其数据集没做封装,仍需直接调用mysql c api对其进行处理,本人将下次完成,不管如何,欢迎大家多提宝贵的意见!
//CDataBase.h
#ifndef CDATABASE_H
#define CDATABASE_H
#include "stdio.h"
#include "/usr/include/mysql/mysql.h"
#include
class CDataBase
{
public:
CDataBase(const char *host, const char *user, const char *passwd, const char *db);
~CDataBase();
bool ExecuteSql(const char* chSql);
MYSQL_RES * OpenRecordset(const char* chSql);
void FreeResult(MYSQL_RES *result);
private:
MYSQL mysql;
bool _bOpen;
const char *_host;
const char *_user;
const char *_passwd;
const char *_db;
protected:
bool Open();
void Close();
bool GetConState();
};
#endif
------------------------------------------
//CDataBase.cpp
#include "cdatabase.h"
CDataBase::CDataBase(const char *host, const char *user, const char *passwd, const char *db)
{
_host = host;
_user = user;
_passwd = passwd;
_db = db;
_bOpen = false;
Open();
}
CDataBase::~CDataBase()
{
Close();
}
bool CDataBase::GetConState()
{
return _bOpen;
}
bool CDataBase::Open()
{
if( !mysql_init(&mysql) )
{
printf("nFailed to initate MySQL connection");
return false;
}
if (!mysql_real_connect(&mysql,_host,_user,_passwd,_db,0,NULL,0))
{
printf( "Failed to connect to MySQL: Error: %sn", mysql_error(&mysql));
return false;
}
printf("Logged on to database sucessfullyn");
_bOpen = true;
return _bOpen;
}
void CDataBase::Close()
{
if(_bOpen)
{
mysql_close(&mysql);
_bOpen = false;
}
}
bool CDataBase::ExecuteSql(const char* chSql)
{
if(!GetConState())
return false;
if(mysql_real_query(&mysql,chSql,strlen(chSql))==0)
return true;
}
MYSQL_RES * CDataBase::OpenRecordset(const char* chSql)
{
MYSQL_RES *rs = NULL;
if(ExecuteSql( chSql ))
{
rs = mysql_store_result(&mysql);
}
return rs;
}
void CDataBase::FreeResult(MYSQL_RES *result)
{
if(result)
mysql_free_result(result);
}
-------------------------------------------------
//Test.cpp
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
#include "cdatabase.h"
using namespace std;
int main(int argc, char *argv[])
{
const char *host = "127.0.0.1";
const char *user = "root";
const char *pwd = NULL;
const char *dbn = "test";
//构造对象
CDataBase *db = new CDataBase(host,user,pwd,dbn);
//创建一个张
db->ExecuteSql( "create table tt1(id int ,name varchar(20))");
//插入一个数据
db->ExecuteSql( "insert into tt1(id,name)values('31','abc')");
//查询数据
MYSQL_RES *res = NULL;
MYSQL_ROW row;
res = db->OpenRecordset("select * from tt");
if(res)
{
cout
//CDataBase.h
#ifndef CDATABASE_H
#define CDATABASE_H
#include "stdio.h"
#include "/usr/include/mysql/mysql.h"
#include
class CDataBase
{
public:
CDataBase(const char *host, const char *user, const char *passwd, const char *db);
~CDataBase();
bool ExecuteSql(const char* chSql);
MYSQL_RES * OpenRecordset(const char* chSql);
void FreeResult(MYSQL_RES *result);
private:
MYSQL mysql;
bool _bOpen;
const char *_host;
const char *_user;
const char *_passwd;
const char *_db;
protected:
bool Open();
void Close();
bool GetConState();
};
#endif
------------------------------------------
//CDataBase.cpp
#include "cdatabase.h"
CDataBase::CDataBase(const char *host, const char *user, const char *passwd, const char *db)
{
_host = host;
_user = user;
_passwd = passwd;
_db = db;
_bOpen = false;
Open();
}
CDataBase::~CDataBase()
{
Close();
}
bool CDataBase::GetConState()
{
return _bOpen;
}
bool CDataBase::Open()
{
if( !mysql_init(&mysql) )
{
printf("nFailed to initate MySQL connection");
return false;
}
if (!mysql_real_connect(&mysql,_host,_user,_passwd,_db,0,NULL,0))
{
printf( "Failed to connect to MySQL: Error: %sn", mysql_error(&mysql));
return false;
}
printf("Logged on to database sucessfullyn");
_bOpen = true;
return _bOpen;
}
void CDataBase::Close()
{
if(_bOpen)
{
mysql_close(&mysql);
_bOpen = false;
}
}
bool CDataBase::ExecuteSql(const char* chSql)
{
if(!GetConState())
return false;
if(mysql_real_query(&mysql,chSql,strlen(chSql))==0)
return true;
}
MYSQL_RES * CDataBase::OpenRecordset(const char* chSql)
{
MYSQL_RES *rs = NULL;
if(ExecuteSql( chSql ))
{
rs = mysql_store_result(&mysql);
}
return rs;
}
void CDataBase::FreeResult(MYSQL_RES *result)
{
if(result)
mysql_free_result(result);
}
-------------------------------------------------
//Test.cpp
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
#include "cdatabase.h"
using namespace std;
int main(int argc, char *argv[])
{
const char *host = "127.0.0.1";
const char *user = "root";
const char *pwd = NULL;
const char *dbn = "test";
//构造对象
CDataBase *db = new CDataBase(host,user,pwd,dbn);
//创建一个张
db->ExecuteSql( "create table tt1(id int ,name varchar(20))");
//插入一个数据
db->ExecuteSql( "insert into tt1(id,name)values('31','abc')");
//查询数据
MYSQL_RES *res = NULL;
MYSQL_ROW row;
res = db->OpenRecordset("select * from tt");
if(res)
{
cout