Mysql API之C++封装(二)
前一篇文章是简单的连接数据库,查询数据库里面的记录,如果需要对记录的数据进行类型转换就比较麻烦了,下面的类可以帮助你利用struct很快的得到你想要的数据,参考如下。DBClient_Struct.h:
#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;\my_bool _is_null;\unsigned long _length;\public:\ bind_##h(){\ int i=0;\ memset(_bind, 0x00, sizeof(_bind));#define BIND_BIN(x,l)\_bind.buffer_type= MYSQL_TYPE_STRING;\_bind.buffer= (char *)&(parent::x);\_bind.buffer_length= l;\_bind.is_null= _is_null+i;\_bind.length= _length+i;\++i;#define BIND_INT(x)\_bind.buffer_type= MYSQL_TYPE_LONG;\_bind.buffer= (char *)&(parent::x);\_bind.buffer_length= 0;\_bind.is_null= _is_null+i;\_bind.length= _length+i;\++i;#define BIND_TINY(x)\_bind.buffer_type= MYSQL_TYPE_TINY;\_bind.buffer= (char *)&(parent::x);\_bind.buffer_length= 0;\_bind.is_null= _is_null+i;\_bind.length= _length+i;\++i;#define BIND_SHORT(x)\_bind.buffer_type= MYSQL_TYPE_SHORT;\_bind.buffer= (char *)&(parent::x);\_bind.buffer_length= 0;\_bind.is_null= _is_null+i;\_bind.length= _length+i;\++i;#define END_BIND(h) }\operator MYSQL_BIND*(){\return _bind;\}\};#endif
DBClient_Struct.cpp:
#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如下:
typedef struct {uint16conn;uint8role;uint8paired; uint8bonded;uint8authenticated;uint8encrypted;uint8sec_mode;uint8sec_level;uint16conn_interval;uint16conn_latency;uint16super_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);}; }
这个类的只支持简单的结构类型,不支持嵌套的结构,希望高手能够帮忙实现,谢谢!
页:
[1]