---
title: "Mysql API之C++封装(二)"
author: "lppchina"
date: "2011-02-14T16:00:54Z"
tags: []
categories: ["数据库"]
slug: "mysql-api-c"
draft: false
lastmod: "2011-02-14T16:00:54Z"
id: 906009
url: "https://mmarket.iteye.com/blog/906009"
---

前一篇文章是简单的连接数据库，查询数据库里面的记录，如果需要对记录的数据进行类型转换就比较麻烦了，下面的类可以帮助你利用struct很快的得到你想要的数据，参考如下。

DBClient_Struct.h:

```java

#ifndef __DB_CLIENT_STRUCT_H__
#define __DB_CLIENT_STRUCT_H__

#include <mysql.h>
#include <string>

using namespace std;

//typedef std::string string; 

struct mysql_parm{ 
	string host;
	string user;
	string password;
	string database;
	int port;
};

class DBSTMT;
class DBMysql;

class DBSTMT{
	DBSTMT(const DBSTMT&);
	DBSTMT& operator=(const DBSTMT&);
	MYSQL_STMT* stmt_; 
public: 
	DBSTMT(char* query,DBMysql& mysql); 
	void execute(){ 
		if(mysql_stmt_execute(stmt_)) 
			throw mysql_stmt_error(stmt_); 
	} 

	void execute(MYSQL_BIND* bind){ 
		if(mysql_stmt_execute(stmt_)) 
			throw mysql_stmt_error(stmt_); 
		if(mysql_stmt_bind_result(stmt_,bind)){ 
			throw mysql_stmt_error(stmt_); 
		} 
		if(mysql_stmt_store_result(stmt_)) 
			throw mysql_stmt_error(stmt_); 
	} 

	//void execute(){ 
	// if(mysql_stmt_execute(stmt_)) 
	// throw mysql_stmt_error(stmt_); 
	//} 

	void bind(MYSQL_BIND* bind){ 
		if(mysql_stmt_bind_param(stmt_,bind) ) 
			throw mysql_stmt_error(stmt_); 
	} 

	int fetch(){ 
		return mysql_stmt_fetch(stmt_)==0; 
	} 
	~DBSTMT(){ 
		if(stmt_){ 
			mysql_stmt_close(stmt_); 
		} 
	} 

}; 


class DBMysql{ 
	DBMysql(const DBMysql&); 
	DBMysql&operator=(const DBMysql&); 
	MYSQL * mysqlPtr_; 

	long errno_; 
protected: 
	friend class DBSTMT; 
	MYSQL_STMT* _createSTMT(){ 
		MYSQL_STMT *ret=mysql_stmt_init(mysqlPtr_); 
		if(ret) 
			return ret; 
		errno_=mysql_errno(mysqlPtr_); 
		throw mysql_error(mysqlPtr_); 
	} 
public: 
	const char* strerr(){ 
		return mysql_error(mysqlPtr_); 
	} 
	DBMysql():mysqlPtr_(NULL){ 
		mysqlPtr_=mysql_init(NULL); 
		if(NULL== mysqlPtr_) 
			throw "Mysql :outof memory"; 
	} 
	void open(const mysql_parm& parm){ 
		if(!mysql_real_connect(mysqlPtr_, 
			parm.host.c_str(), 
			parm.user.c_str(), 
			parm.password.c_str(), 
			parm.database.c_str(), 
			0, 
			0, 
			0 )) 
		{ 
			errno_=mysql_errno(mysqlPtr_); 
			throw(mysql_error(mysqlPtr_)); 
		} 
	} 

	void close(){ 
		if(mysqlPtr_) 
		{ 
			mysql_close(mysqlPtr_); 
			mysqlPtr_=NULL; 
		} 
	} 
};

#define DECL_BIND(h,n)\
class bind_##h:public h{\
	typedef h parent;\
	MYSQL_BIND _bind[n];\
	my_bool _is_null[n];\
	unsigned long _length[n];\
public:\
	   bind_##h(){\
	   int i=0;\
	   memset(_bind, 0x00, sizeof(_bind));

#define BIND_BIN(x,l)\
	_bind.buffer_type= MYSQL_TYPE_STRING;\
	_bind[i].buffer= (char *)&(parent::x);\
	_bind[i].buffer_length= l;\
	_bind[i].is_null= _is_null+i;\
	_bind[i].length= _length+i;\
	++i;

#define BIND_INT(x)\
	_bind[i].buffer_type= MYSQL_TYPE_LONG;\
	_bind[i].buffer= (char *)&(parent::x);\
	_bind[i].buffer_length= 0;\
	_bind[i].is_null= _is_null+i;\
	_bind[i].length= _length+i;\
	++i;

#define BIND_TINY(x)\
	_bind[i].buffer_type= MYSQL_TYPE_TINY;\
	_bind[i].buffer= (char *)&(parent::x);\
	_bind[i].buffer_length= 0;\
	_bind[i].is_null= _is_null+i;\
	_bind[i].length= _length+i;\
	++i;

#define BIND_SHORT(x)\
	_bind[i].buffer_type= MYSQL_TYPE_SHORT;\
	_bind[i].buffer= (char *)&(parent::x);\
	_bind[i].buffer_length= 0;\
	_bind[i].is_null= _is_null+i;\
	_bind[i].length= _length+i;\
	++i;

#define END_BIND(h) }\
	operator MYSQL_BIND*(){\
	return _bind;\
	}\
};

#endif

```


DBClient_Struct.cpp:

```java

#include "DBClient_Struct.h"

DBSTMT::DBSTMT(char* query,DBMysql& mysql):stmt_(NULL){ 
	stmt_=mysql._createSTMT(); 
	if(!stmt_)
		throw mysql.strerr();
	if( mysql_stmt_prepare(stmt_,query,strlen(query)) ) 
	{
		throw mysql_stmt_error(stmt_); 
	} 
}

```


实例Code如下：

```java

typedef struct {
	uint16		conn;
	uint8		role;
	uint8		paired;
         uint8		bonded;
	uint8		authenticated;
	uint8		encrypted;
	uint8		sec_mode;
	uint8		sec_level;
	uint16		conn_interval;
	uint16		conn_latency;
	uint16		super_timeout;
} gap_link_status_t_4DB;

DECL_BIND(gap_link_status_t_4DB,11) 
	BIND_SHORT(conn)
	BIND_TINY(role)
	BIND_TINY(paired)
	BIND_TINY(bonded)
	BIND_TINY(authenticated)
	BIND_TINY(encrypted)
	BIND_TINY(sec_mode)
	BIND_TINY(sec_level)
	BIND_SHORT(conn_interval)
	BIND_SHORT(conn_latency)
	BIND_SHORT(super_timeout)
END_BIND(gap_link_status_t_4DB)

int main()
{
    	DBMysql dbmysql;
	mysql_parm dbparm;
	dbparm.host="localhost";
	dbparm.user="root"; 
	dbparm.password="t-span"; 
	dbparm.port = 0;
	dbparm.database="test";
	dbmysql.open(dbparm);

	DBSTMT smt("select conn,role,paired,bonded,authenticated,encrypted,sec_mode,sec_level,conn_interval,conn_latency,super_timeout\
			   from gap_link_status_t where id = 0;",dbmysql);

	bind_gap_link_status_t_4DB tmp_link; 
	smt.execute(tmp_link);

	while(smt.fetch()){
             printf("conn=%d, role=%d\n", tmp_link.conn, tmp_link.role);
	}; 
}

```


这个类的只支持简单的结构类型，不支持嵌套的结构，希望高手能够帮忙实现，谢谢！