From 7598ed496d61d843706fea44dcf64dae64992280 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 7 Aug 2017 10:55:28 +0800 Subject: [PATCH] Update MySQLConn --- MySQLWrapper.cpp | 32 ++++++++++++++++++++++++++++++-- MySQLWrapper.h | 6 ++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/MySQLWrapper.cpp b/MySQLWrapper.cpp index c2ae5ee..9b90daa 100644 --- a/MySQLWrapper.cpp +++ b/MySQLWrapper.cpp @@ -1,5 +1,11 @@ #include "MySQLWrapper.h" #include "MySQLInclude.h" +#include +using namespace std; + +/********************** +MySQL Result Class. +**********************/ struct MySQLResult::impl { @@ -48,9 +54,15 @@ void MySQLResult::stepRow(const std::functionm),host,user,passwd,db,port,NULL,0)==NULL)?0:-1; + if(mysql_real_connect(&(pimpl->m),host,user,passwd,db,port,NULL,0)==NULL) + { + pimpl->lasterr=mysql_error(&(pimpl->m)); + return -1; + } + else + { + return 0; + } } int MySQLConn::exec(const std::string& SQLCommand, const std::function& func) @@ -73,8 +93,10 @@ int MySQLConn::exec(const std::string& SQLCommand, const std::functionm),SQLCommand.c_str(),SQLCommand.size())!=0) { /// Failed to Query + pimpl->lasterr=mysql_error(&(pimpl->m)); return -1; } + MYSQL_RES* pres=mysql_store_result(&(pimpl->m)); if(pres==nullptr) { @@ -87,6 +109,7 @@ int MySQLConn::exec(const std::string& SQLCommand, const std::functionlasterr=mysql_error(&(pimpl->m)); return -2; } } @@ -94,7 +117,7 @@ int MySQLConn::exec(const std::string& SQLCommand, const std::functionres=pres; - func(res); + if(func) func(res); /// MySQL_RES will be released normally by MySQLResult::~MySQLResult() return 0; @@ -109,3 +132,8 @@ uint64_t MySQLConn::getAffectedRows() { return mysql_affected_rows(&(pimpl->m)); } + +const std::string& MySQLConn::getError() +{ + return pimpl->lasterr; +} diff --git a/MySQLWrapper.h b/MySQLWrapper.h index 67dabb0..c580474 100644 --- a/MySQLWrapper.h +++ b/MySQLWrapper.h @@ -32,13 +32,15 @@ public: ~MySQLConn(); /// 数据库连接 + /// 返回值: 0:连接正常, -1:连接错误 int connect(const char* host,const char* user,const char* passwd,const char* db,unsigned int port); /// 执行SQL语句 - int exec(const std::string& SQLCommand,const std::function& res); + /// 返回值: 1: 语句执行正常,无返回结果(func未执行) 0:语句执行正常,返回结果集交付func执行. -1:语句执行错误.(mysql_real_query失败) -2: 语句执行正确,获取结果出现错误 + int exec(const std::string& SQLCommand,const std::function& func); /// 获取错误信息 - const char* getError(); + const std::string& getError(); /// 当结果为空时用于判断数据库运行正常与否 unsigned int getFieldCount();