第二個庫:libdb,封裝了MySQL的c api
第一個工具類:DBOperator,比較純粹的,就是封裝MySql的c api
主要是為了提供一些更友好一些的接口,供上層邏輯使用
封裝了出錯時的錯誤信息輸出到log(log使用的是之前工具類庫中的Logger)
提供兩類接口:
1、直接執(zhí)行sql的方式:
直接執(zhí)行sql的方式就是,每次都需要進行字符串拼接,
用每一個字段的值,想要進行的操作,拼接一個完整的sql語句。
拼接之后,交給mysql。
mysql收到sql字符串之后,需要先對字符串進行解析,再執(zhí)行。
2、預處理sql的方式:
預處理sql的方式就是,先將sql語句中字段的值,以變量的形式(用一個問號'?'表示),先占位,組成一個帶占位符的sql語句,
然后就交給mysql去解析字符串。
在此之后的每次執(zhí)行,不需要再次對sql字符串做解析,只需要對變量賦值,就可以執(zhí)行得出結果。
一般推薦都是采用第二種方式。
提供的主要功能:
| 功能說明 | 對應封裝的c api | |
| 1、建立,斷開數(shù)據(jù)庫連接 | mysql_real_connect,mysql_close | |
| 2、維持數(shù)據(jù)庫鏈接心跳 | mysql_ping | |
| 3、直接執(zhí)行sql | (1)、執(zhí)行sql語句 | mysql_real_query |
| (2)、執(zhí)行sql之后,如果是查詢,獲取結果集 | mysql_store_result | |
| (3)、獲取結果集之后,獲取結果集的列數(shù) | mysql_num_fields | |
| (4)、獲取結果集之后,獲取其中一條結果數(shù)據(jù)(數(shù)據(jù)以字符串數(shù)組的形式表現(xiàn)) | mysql_fetch_row | |
| (5)、獲取一條結果數(shù)據(jù)之后,獲取每一列的數(shù)據(jù)長度(字符串形式的長度) | mysql_fetch_lengths | |
| (6)、拼接sql語句時,執(zhí)行sql之前,如有必要,對sql字符串進行安全轉(zhuǎn)換,防止sql注入 | mysql_real_escape_string | |
| 4、預處理sql | (1)、預處理sql語句 | mysql_stmt_init,mysql_stmt_PRepare |
| (2)、綁定sql語句的參數(shù) | mysql_stmt_bind_param | |
| (3)、綁定sql語句的查詢結果 | mysql_stmt_bind_result | |
| (4)、具體綁定某一類型的值(由于本人的業(yè)務需要,目前僅提供支持7種數(shù)據(jù)類型:int,unsigned int,long long,unsigned long long,float,字符串,Blob) | MYSQL_BIND結構賦值 | |
| (5)、真實執(zhí)行預處理sql | mysql_stmt_execute | |
| (6)、執(zhí)行sql之后,如果是查詢,獲取結果集 | mysql_stmt_result_metadata,mysql_stmt_store_result | |
| (7)、獲取結果集之后,獲取其中一條結果數(shù)據(jù)(數(shù)據(jù)將直接返回到(4)中綁定的對應內(nèi)存中) | mysql_stmt_fetch | |
| (8)、關閉,釋放預處理句柄 | mysql_stmt_close | |
| (9)、獲取錯誤信息 | mysql_stmt_errno,mysql_stmt_error | |
| 5、獲取結果集之后,釋放結果集(以上兩種方式通用) | mysql_free_result | |
| 6、獲取錯誤信息 | mysql_errno,mysql_error | |
上代碼:
DBOperator.h
#ifndef __DBOperator_h__#define __DBOperator_h__struct st_mysql;typedef struct st_mysql MYSQL;struct st_mysql_stmt;typedef struct st_mysql_stmt MYSQL_STMT;struct st_mysql_bind;typedef struct st_mysql_bind MYSQL_BIND;struct st_mysql_res;typedef struct st_mysql_res MYSQL_RES;typedef char** MYSQL_ROW;namespace common{ namespace db{ class DBOperator { public: DBOperator(); ~DBOperator(); //獲取錯誤信息 static void GetErrorInfo(MYSQL* mysql); static void GetStmtErrorInfo(MYSQL_STMT* stmt); //連接數(shù)據(jù)庫 static MYSQL* Connect(const char *host, unsigned int port, const char *user, const char *passwd, const char *db, const char *charset = "utf8"); //斷開連接 static void DisConnect(MYSQL* mysql); //ping static bool Ping(MYSQL* mysql); /////////////////////直接執(zhí)行SQL的方式///////////////////// //轉(zhuǎn)換sql字符 static bool MakeSafeStr(MYSQL* mysql, char *to, int toLen, const char *from, int fromLen); //執(zhí)行sql static bool ExecQuery(MYSQL* mysql, const char *sql); //查詢/獲取查詢結果集 static MYSQL_RES* SelectResult(MYSQL* mysql); static MYSQL_ROW GetNextResult(MYSQL_RES* res); //獲取結果集的列數(shù) static unsigned int GetResultFields(MYSQL_RES* res); //獲取結果集的每個字段的(字符串形式的)長度 static unsigned long* GetResultLens(MYSQL_RES* res); //釋放結果集(預處理方式下通用) static void FreeResult(MYSQL_RES* pRes); /////////////////////預處理執(zhí)行SQL的方式///////////////////// //預處理句柄 static MYSQL_STMT* ExecPrepare(MYSQL* mysql, const char *sql); //綁定字段 static bool BindInt(MYSQL_STMT* stmt, MYSQL_BIND* bind, int *value); static bool BindUInt(MYSQL_STMT* stmt, MYSQL_BIND* bind, unsigned int *value); static bool BindInt64(MYSQL_STMT* stmt, MYSQL_BIND* bind, long long *value); static bool BindUInt64(MYSQL_STMT* stmt, MYSQL_BIND* bind, unsigned long long *value); static bool BindFloat(MYSQL_STMT* stmt, MYSQL_BIND* bind, float *value); static bool BindString(MYSQL_STMT* stmt, MYSQL_BIND* bind, char *value, unsigned long *len); static bool BindBlob(MYSQL_STMT* stmt, MYSQL_BIND* bind, void *value, unsigned long *len); //綁定參數(shù)/結果 static bool BindResultStmt(MYSQL_STMT* stmt, MYSQL_BIND* bind); static bool BindParameterStmt(MYSQL_STMT* stmt, MYSQL_BIND* bind); //執(zhí)行預處理sql static bool ExecStmt(MYSQL_STMT* stmt); //查詢/獲取查詢結果 static MYSQL_RES* SelectResult(MYSQL_STMT* stmt); static bool GetNextResult(MYSQL_STMT* stmt); //關閉預處理句柄 static bool FreeStmt(MYSQL_STMT* stmt); }; }}#endifDBOperator.cpp#include "DBOperator.h"#ifdef WIN32#include <winsock2.h>#endif#include <stdio.h>#include <mysql.h>#include <string.h>#include "Logger.h"using namespace common::tool;#include "DBDefine.h"namespace common{ namespace db{ DBOperator::DBOperator() { } DBOperator::~DBOperator() { } void DBOperator::GetErrorInfo(MYSQL* mysql) { LOG_INFO(g_LibDBLog) << "error : " << mysql_errno(mysql) << ", " << mysql_error(mysql); } void DBOperator::GetStmtErrorInfo(MYSQL_STMT* stmt) { LOG_INFO(g_LibDBLog) << "error : " << mysql_stmt_errno(stmt) << ", " << mysql_stmt_error(stmt); } //申請數(shù)據(jù)源的連接 MYSQL* DBOperator::Connect(const char *host, unsigned int port, const char *user, const char *passwd, const char *db, const char *charset) { MYSQL* mysql = mysql_init(NULL); if (NULL != mysql) { mysql_options(mysql, MYSQL_SET_CHARSET_NAME, charset); mysql = mysql_real_connect(mysql, host, user, passwd, db, port, NULL, 0); if (NULL != mysql) { return mysql; } else { GetErrorInfo(mysql); return NULL; } } else { GetErrorInfo(mysql); return NULL; } } void DBOperator::DisConnect(MYSQL* mysql) { if (NULL != mysql) { mysql_close(mysql); } } bool DBOperator::Ping(MYSQL* mysql) { if (NULL != mysql) { if (0 == mysql_ping(mysql)) { return true; } else { GetErrorInfo(mysql); return false; } } else { return false; } } bool DBOperator::MakeSafeStr(MYSQL* mysql, char *to, int toLen, const char *from, int fromLen) { if (NULL != mysql) { memset(to, 0x00, sizeof(char)* toLen); if (0 != mysql_real_escape_string(mysql, to, from, fromLen)) { return true; } else { GetErrorInfo(mysql); return false; } } else { return false; } } bool DBOperator::ExecQuery(MYSQL* mysql, const char *sql) { if (NULL != mysql && NULL != sql) { if (0 == mysql_real_query(mysql, sql, strlen(sql))) { return true; } else { GetErrorInfo(mysql); return false; } } else { return false; } } MYSQL_RES* DBOperator::SelectResult(MYSQL* mysql) { if (NULL != mysql) { return mysql_store_result(mysql); } else { return NULL; } } MYSQL_ROW DBOperator::GetNextResult(MYSQL_RES* res) { if (NULL != res) { return mysql_fetch_row(res); } else { return NULL; } } unsigned int DBOperator::GetResultFields(MYSQL_RES* res) { if (NULL != res) { return mysql_num_fields(res); } else { return 0; } } unsigned long* DBOperator::GetResultLens(MYSQL_RES* res) { if (NULL != res) { return mysql_fetch_lengths(res); } else { return NULL; } } void DBOperator::FreeResult(MYSQL_RES* res) { if (NULL != res) { mysql_free_result(res); } } MYSQL_STMT* DBOperator::ExecPrepare(MYSQL* mysql, const char *sql) { if (NULL != mysql) { MYSQL_STMT* stmt = mysql_stmt_init(mysql); if (NULL != stmt) { if (0 == mysql_stmt_prepare(stmt, sql, strlen(sql))) { return stmt; } else { GetStmtErrorInfo(stmt); return NULL; } } else { GetErrorInfo(mysql); return NULL; } } else { return NULL; } } bool DBOperator::BindInt(MYSQL_STMT* stmt, MYSQL_BIND* bind, int *value) { if (NULL != stmt && NULL != bind && NULL != value) { memset(bind, 0, sizeof(MYSQL_BIND)); bind->buffer_type = MYSQL_TYPE_LONG; bind->buffer = (char *)value; bind->is_unsigned = false; return true; } else { return false; } } bool DBOperator::BindUInt(MYSQL_STMT* stmt, MYSQL_BIND* bind, unsigned int *value) { if (NULL != stmt && NULL != bind && NULL != value) { memset(bind, 0, sizeof(MYSQL_BIND)); bind->buffer_type = MYSQL_TYPE_LONG; bind->buffer = (char *)value; bind->is_unsigned = true; return true; } else { return false; } } bool DBOperator::BindInt64(MYSQL_STMT* stmt, MYSQL_BIND* bind, long long *value) { if (NULL != stmt && NULL != bind && NULL != value) { memset(bind, 0, sizeof(MYSQL_BIND)); bind->buffer_type = MYSQL_TYPE_LONGLONG; bind->buffer = (char *)value; bind->is_unsigned = false; return true; } else { return false; } } bool DBOperator::BindUInt64(MYSQL_STMT* stmt, MYSQL_BIND* bind, unsigned long long *value) { if (NULL != stmt && NULL != bind && NULL != value) { memset(bind, 0, sizeof(MYSQL_BIND)); bind->buffer_type = MYSQL_TYPE_LONGLONG; bind->buffer = (char *)value; bind->is_unsigned = true; return true; } else { return false; } } bool DBOperator::BindFloat(MYSQL_STMT* stmt, MYSQL_BIND* bind, float *value) { if (NULL != stmt && NULL != bind && NULL != value) { memset(bind, 0, sizeof(MYSQL_BIND)); bind->buffer_type = MYSQL_TYPE_FLOAT; bind->buffer = (char *)value; return true; } else { return false; } } bool DBOperator::BindString(MYSQL_STMT* stmt, MYSQL_BIND* bind, char *value, unsigned long *len) { if (NULL != stmt && NULL != bind && NULL != value && NULL != len) { memset(bind, 0, sizeof(MYSQL_BIND)); bind->buffer_type = MYSQL_TYPE_STRING; bind->buffer = (char *)value; bind->buffer_length = *len; bind->length = len; return true; } else { return false; } } bool DBOperator::BindBlob(MYSQL_STMT* stmt, MYSQL_BIND* bind, void *value, unsigned long *len) { if (NULL != stmt && NULL != bind && NULL != value && NULL != len) { memset(bind, 0, sizeof(MYSQL_BIND)); bind->buffer_type = MYSQL_TYPE_BLOB; bind->buffer = (char *)value; bind->buffer_length = *len; bind->length = len; return true; } else { return false; } } bool DBOperator::BindParameterStmt(MYSQL_STMT* stmt, MYSQL_BIND* bind) { if (NULL != stmt && NULL != bind) { if (0 == mysql_stmt_bind_param(stmt, bind)) { return true; } else { GetStmtErrorInfo(stmt); return false; } } else { return false; } } bool DBOperator::BindResultStmt(MYSQL_STMT* stmt, MYSQL_BIND* bind) { if (NULL != stmt && NULL != bind) { if (0 == mysql_stmt_bind_result(stmt, bind)) { return true; } else { GetStmtErrorInfo(stmt); return false; } } else { return false; } } bool DBOperator::ExecStmt(MYSQL_STMT* stmt) { if (NULL != stmt) { if (0 == mysql_stmt_execute(stmt)) { return true; } else { GetStmtErrorInfo(stmt); return false; } } else { return false; } } MYSQL_RES* DBOperator::SelectResult(MYSQL_STMT* stmt) { if (NULL != stmt) { MYSQL_RES* res = mysql_stmt_result_metadata(stmt); if (NULL != res) { if (0 == mysql_stmt_store_result(stmt)) { return res; } else { GetStmtErrorInfo(stmt); return NULL; } } else { return NULL; } } else { return NULL; } } bool DBOperator::GetNextResult(MYSQL_STMT* stmt) { if (NULL != stmt) { if (0 == mysql_stmt_fetch(stmt)) { return true; } else { return false; } } else { return false; } } bool DBOperator::FreeStmt(MYSQL_STMT* stmt) { if (NULL != stmt) { if (0 == mysql_stmt_close(stmt)) { return true; } else { GetStmtErrorInfo(stmt); return false; } } else { return false; } } }}DBDefine.h
#ifndef __DBDefine_h__#define __DBDefine_h__namespace common{ namespace tool{ class Logger; }}//libdb專用logextern common::tool::Logger g_LibDBLog;#endifDBDefine.cpp#include "DBDefine.h"#include "Logger.h"using namespace common::tool;//libdb專用logLogger g_LibDBLog("LibDB");
新聞熱點
疑難解答