c mysql操作库_c/c++操作mysql数据库

1.用到的数据结构

#ifndef GLOBAL_H

#define GLOBAL_H

#include

using std::string;

struct Data_Param //数据库操作参数

{

string db_name; //数据库名

string tab_name; //表名

string col_name; //列名

string select_exp; //选择表达式

string where_def; //选择限定条件

string insert_val; //插入值

string set_exp; //更新数据库内容的表达式

};

struct Database_Param //数据库参数

{

string host; //主机名

string user; //用户名

string password; //密码

string db; //数据库名

unsigned int port; //端口,一般为0

const char* unix_socket; //套接字,一般为NULL

unsigned int client_flag; //一般为0

Database_Param(string host, string user, string password, string db, unsigned int port, const char* unix_socket, unsigned int client_flag)

{

this->host = host;

this->user = user;

this->password = password;

this->db = db;

this->port = port;

this->unix_socket = unix_socket;

this->client_flag = client_flag;

}

};

#endif

2.CMysql类的声名

/*This is a C++ incapsulation of the mysql C API

*In order to use this:

* 1.set the project include directory includes the include directory under mysql directory

* 2.set the project library directory includes the lib/opt directory under mysql directory

* 3.set the addtional dependency of the linker of the project 'libmysql.lib'

*After all of the above work, you can simply add this CMysql class in your project,and can conviently use it

*If you find any bugs, please connect me : gswzsh@126.com, thank you

*/

#ifndef CMYSQL_H

#define CMYCQL_H

#include

#include

#include "global.h"

#include

class CMysql

{

public:

string GetState(); //服务器状态

string GetServerInfo(); //服务器信息

int GetProtocolInfo(); //协议信息

string GetHostInfo(); //主机信息

string GetClientInfo(); //客户机信息

string GetFieldName(int FieldNum);

bool IsEnd(); //是否最后

void SeekData(int offset); //查找指定数据

unsigned int GetFieldNum(); //得到字段数

bool ConnectDB(Database_Param* p); //连接数据库

MYSQL_ROW GetRecord(); //得到结果(一个记录)

my_ulonglong GetRowNum(); //得到记录数

bool SelectDB(Data_Param* para); //选择数据库

bool UpdateRecord(Data_Param* para); //更新记录

bool SelectRecord(Data_Param* para); //选择记录

bool InsertRecord(Data_Param* para); //插入记录

bool DelRecord(Data_Param* para); //删除记录

bool SelectAll(Data_Param* para); //选择所有记录

string OutErrors(); //输出错误信息

bool MysqlEx(MYSQL* mysql, string str);

CMysql();

virtual ~CMysql();

private:

bool FindSave(string str); //查找并保存结果集

private:

MYSQL mysql; //数据库链接句柄

MYSQL_FIELD* field; //字段信息(结构体)

public:

MYSQL_RES* query; //结果集

MYSQL_ROW row; //记录集

};

#endif

3.CMysql类的实现

#include "CMysql.h"

using std::ostringstream;

CMysql::CMysql()

{

mysql_init(&mysql);

}

CMysql::~CMysql()

{

mysql_close(&mysql);

}

//Connect to the DB host

bool CMysql::ConnectDB(Database_Param* p)

{

if(!mysql_real_connect(&mysql,(p->host).c_str(),p->user.c_str(),p->password.c_str(),p->db.c_str(),p->port,p->unix_socket,p->client_flag))

{

return false;

}

return true;

}

//Select DB

bool CMysql::SelectDB(Data_Param* para)

{

if(mysql_select_db(&mysql,para->db_name.c_str()))

return false;

else

return true;

}

//Get the record and store it int 'row'

MYSQL_ROW CMysql::GetRecord()

{

return (row=mysql_fetch_row(query));

}

//A utility to select all the records

bool CMysql::SelectAll(Data_Param* para)

{

unsigned int i=0;

ostringstream Ostr;

Ostr << "select * from " << para->tab_name;

if(!FindSave(Ostr.str()))

{

return false;

}

return true;

}

//Seek the row

void CMysql::SeekData(int offset)

{

mysql_data_seek(query,offset);

}

//Execute the query and store the results in 'query'

bool CMysql::FindSave(string str)

{

if(mysql_query(&mysql,str.c_str()))

return false;

query=mysql_store_result(&mysql);

return true;

}

//A utility to delete the records

bool CMysql::DelRecord(Data_Param* para)

{

ostringstream Ostr;

Ostr << "delete from " << para->tab_name << " where " << para->where_def;

if(mysql_query(&mysql,Ostr.str().c_str()))

{

return false;

}

return true;

}

//A utility to insert records

bool CMysql::InsertRecord(Data_Param* para)

{

ostringstream Ostr;

Ostr << "insert into " << para->tab_name << " values(" << para->insert_val << ")";

if(mysql_query(&mysql,Ostr.str().c_str()))

{

return false;

}

return true;

}

//A utility to select records

bool CMysql::SelectRecord(Data_Param* para)

{

unsigned int i=0;

ostringstream Ostr;

Ostr << "select " << para->select_exp << " from " << para->tab_name << " where " << para->where_def;

if(!FindSave(Ostr.str()))

{

return false;

}

return true;

}

//A utility to update records

bool CMysql::UpdateRecord(Data_Param* para)

{

ostringstream Ostr;

Ostr << "update " << para->tab_name << " set " << para->set_exp << " where " << para->where_def;

if(mysql_query(&mysql,Ostr.str().c_str()))

{

return false;

}

return true;

}

bool CMysql::IsEnd()

{

if(mysql_eof(query))

{

return true;

}

else

{

return false;

}

}

//Get number of rows

my_ulonglong CMysql::GetRowNum()

{

return (mysql_num_fields(query));

}

//Get the name of the FieldNumth Field

string CMysql::GetFieldName(int FieldNum)

{

field=mysql_fetch_field_direct(query,FieldNum);

return string(field->name);

}

string CMysql::GetClientInfo()

{

return string(mysql_get_client_info());

}

string CMysql::GetServerInfo()

{

return string(mysql_get_server_info(&mysql));

}

string CMysql::GetHostInfo()

{

return string(mysql_get_host_info(&mysql));

}

int CMysql::GetProtocolInfo()

{

return mysql_get_proto_info(&mysql);

}

string CMysql::GetState()

{

string state(mysql_stat(&mysql));

if(!state.empty())

return string(OutErrors());

return state;

}

/*void CMysql::FreeRecord()

{

mysql_free_result(query);

}*/

string CMysql::OutErrors()

{

return string(mysql_error(&mysql));

}

bool CMysql::MysqlEx(MYSQL* mysql, string str)

{

if(!FindSave(str))

{

return false;

}

return true;

}

unsigned int CMysql::GetFieldNum()

{

return (mysql_num_fields(query));

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值