主要用于维护C结构的查询和返回。
#include
typedef std::string string;
struct mysql_parm{
string host;
string user;
string password;
string database;
string unixsock;
};
class DBSTMT;
class DBMysql;
class DBSTMT{
DBSTMT(const DBSTMT&);
DBSTMT& operator=(const DBSTMT&);
MYSQL_STMT* stmt_;
public:
DBSTMT(pcsz_t 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_;
uint32_t 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";
}
&nbs