工程須要加上各個路徑:mysql
庫使用須要幾個文件:
一、include 文件夾 c/c++ /常規/附加包含目錄
Connector/c++ 的安裝版裏面的Include 文件夾。或者把 /driver以及/driver/nativeapi 裏面的頭文件拷貝到一個文件夾裏面(注意nativeapi要更名爲 cppconn)。
二、Connector/c++ 庫文件 和 MySql庫文件:
2.一、mysqlcppconn.dll /debug,exe生成目錄
2.二、mysqlcppconn.lib 連接器/輸入/附加依賴項
2.三、libmysql.dll /debug
三、boost庫所在目錄 c/c++/常規/附加包含目錄
頭文件 MySqlDataBase.h :ios
#ifndef __MYSQL_DATABASE_H #define __MYSQL_DATABASE_H #include "mysql_connection.h" #include "mysql_driver.h" #include "cppconn/prepared_statement.h" #include "cppconn/statement.h" #include <map> typedef boost::scoped_ptr<sql::Connection> Connection; typedef boost::scoped_ptr<sql::PreparedStatement> PreparedStatement; typedef boost::scoped_ptr<sql::Statement> Statement; typedef boost::shared_ptr<sql::ResultSet> ResultSet; typedef sql::mysql::MySQL_Driver MySQL_Driver; //mysql 錯誤類 class CMySqlError { //存儲變量 protected: int m_ErrorCode; std::string m_strErrorDescribe; public: //構造函數 CMySqlError(); //析構函數 ~CMySqlError(); public: //獲取ErrorCode int getErrorCode(){ return m_ErrorCode; } //錯誤描述 std::string GetErrorDestribe(){ return m_strErrorDescribe; } public: //設置錯誤 void SetErrorInfo(sql::SQLException &e); }; class CMySqlDataBase { //信息變量 protected: CMySqlError m_MySqlError; //當前錯誤信息 std::map<std::string, std::string> m_ConnectProperties; //鏈接信息 //狀態變量 protected: const unsigned int m_dwTryConnectTimes; //鏈接變量 protected: Connection m_DBConnect; PreparedStatement m_DBPrepareState; ResultSet m_DBRecordSet; //函數定義 public: //構造函數 CMySqlDataBase(); //析構函數 ~CMySqlDataBase(); //管理接口 public: //打開鏈接 bool OpenConnect(); //關閉記錄 bool CloseRecordset(); //關閉鏈接 bool CloseConnect(); //從新鏈接(未實現) bool TryConnectAgain(); //設置信息 bool SetConnectionInfo(const std::string &hostIp,unsigned short hostPort,const std::string &dataBaseName,const std::string &userName,const std::string &password); //狀態接口(未實現) public: //是否鏈接錯誤 bool IsConnectError(); //是否打開 bool IsRecordsetOpened(); // public: //準備prepareState bool PreparedExcute(const std::string &szCommand); bool setBigInt(unsigned int parameterIndex, const std::string& value); bool setBlob(unsigned int parameterIndex, std::istream * blob); //長文本字符串 bool setBoolean(unsigned int parameterIndex, bool value); bool setDateTime(unsigned int parameterIndex, const std::string& value); bool setDouble(unsigned int parameterIndex, double value); bool setInt(unsigned int parameterIndex, int32_t value); bool setUInt(unsigned int parameterIndex, uint32_t value); bool setInt64(unsigned int parameterIndex, int64_t value); bool setUInt64(unsigned int parameterIndex, uint64_t value); bool setString(unsigned int parameterIndex, const std::string& value); bool setNull(unsigned int parameterIndex, int sqlType); //執行命令(存儲過程) bool ExecuteCommand(bool bRecordset); //執行語句接口 public: //執行查詢(Select) bool Query(const std::string &szCommand); //執行語句(Insert,Update,Delete) bool Execute(const std::string &szCommand); //字段接口 /* next() must been used before getdata */ public: //獲取當前 Result set const ResultSet &GetRecordSet(); //get Next Record set bool GetNextResultSet(); //move resultset to the nth result in the set bool NextFieldExist(); //獲取參數 bool GetFieldValue(const std::string& columnLabel,bool &bValue); //獲取參數 bool GetFieldValue(const std::string& columnLabel,long double &dbValue); //獲取參數 bool GetFieldValue(const std::string& columnLabel,int32_t &nValue); //獲取參數 bool GetFieldValue(const std::string& columnLabel,uint32_t &uValue); //獲取參數 bool GetFieldValue(const std::string& columnLabel,int64_t &llValue); //獲取參數 bool GetFieldValue(const std::string& columnLabel,uint64_t &lluValue); //獲取參數 bool GetFieldValue(const std::string& columnLabel,char szBuffer[],uint32_t uSize); //獲取參數 bool GetFieldValue(const std::string& columnLabel,std::string &szValue); //獲取參數 //bool GetFieldValue(const std::string& columnLabel,SYSTEMTIME &systime); //內部函數 private: //設置錯誤 void SetErrorInfo(sql::SQLException &e); }; #endif
源文件MySqlDataBase.cppc++
#include "MySqlDataBase.h" #include <sstream> CMySqlError::CMySqlError() { } CMySqlError::~CMySqlError() { } void CMySqlError::SetErrorInfo(sql::SQLException &e) { m_ErrorCode = e.getErrorCode(); m_strErrorDescribe = e.what(); throw this; } CMySqlDataBase::CMySqlDataBase():m_DBConnect(NULL),m_DBPrepareState(NULL),m_DBRecordSet((sql::ResultSet*)NULL),m_dwTryConnectTimes(1) { } CMySqlDataBase::~CMySqlDataBase() { try { CloseConnect(); m_DBRecordSet.reset((sql::ResultSet*)NULL); m_DBPrepareState.reset(NULL); m_DBConnect.reset(NULL); } catch(sql::SQLException &e) { SetErrorInfo(e);} } //設置錯誤 void CMySqlDataBase::SetErrorInfo(sql::SQLException &e) { m_MySqlError.SetErrorInfo(e); } //打開鏈接 bool CMySqlDataBase::OpenConnect() { //創建鏈接 try { sql::mysql::MySQL_Driver *driver = sql::mysql::get_mysql_driver_instance(); m_DBConnect.reset(driver->connect(m_ConnectProperties["hostName"],m_ConnectProperties["userName"],m_ConnectProperties["password"])); m_DBConnect->setSchema(m_ConnectProperties["schema"]); } catch(sql::SQLException &e) { SetErrorInfo(e);} return true; } //關閉記錄 bool CMySqlDataBase::CloseRecordset() { try { if(m_DBPrepareState != NULL) { while(m_DBPrepareState->getMoreResults()) m_DBRecordSet.reset(m_DBPrepareState->getResultSet()); m_DBPrepareState.reset(NULL); } if(m_DBRecordSet != NULL) m_DBRecordSet.reset((sql::ResultSet*)NULL); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //關閉鏈接 bool CMySqlDataBase::CloseConnect() { try { CloseRecordset(); //close connect if(m_DBConnect != NULL) { m_DBConnect.reset(NULL); } return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //設置信息 bool CMySqlDataBase::SetConnectionInfo(const std::string &hostIp,unsigned short hostPort,const std::string &dataBaseName,const std::string &userName,const std::string &password) { try { std::stringstream hostss; hostss<<"tcp://"<<hostIp<<":"<<hostPort; m_ConnectProperties["hostName"] = hostss.str(); m_ConnectProperties["userName"] = userName; m_ConnectProperties["password"] = password; m_ConnectProperties["schema"] = dataBaseName; return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //是否打開 bool CMySqlDataBase::IsRecordsetOpened() { if(m_DBRecordSet == NULL) return false; if(m_DBRecordSet->isClosed()) return false; return true; } //準備prepareState bool CMySqlDataBase::PreparedExcute(const std::string &szCommand) { if(szCommand.empty()) return false; //close RecordSet; CloseRecordset(); try { m_DBPrepareState.reset(m_DBConnect->prepareStatement(szCommand)); m_DBPrepareState->clearParameters(); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setBigInt(unsigned int parameterIndex, const std::string& value) { try { m_DBPrepareState->setBigInt(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setBlob(unsigned int parameterIndex, std::istream * value) //長文本字符串 { try { m_DBPrepareState->setBlob(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setBoolean(unsigned int parameterIndex, bool value) { try { m_DBPrepareState->setBoolean(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setDateTime(unsigned int parameterIndex, const std::string& value) { try { m_DBPrepareState->setDateTime(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setDouble(unsigned int parameterIndex, double value) { try { m_DBPrepareState->setDouble(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setInt(unsigned int parameterIndex, int32_t value) { try { m_DBPrepareState->setInt(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setUInt(unsigned int parameterIndex, uint32_t value) { try { m_DBPrepareState->setUInt(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setInt64(unsigned int parameterIndex, int64_t value) { try { m_DBPrepareState->setInt64(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setUInt64(unsigned int parameterIndex, uint64_t value) { try { m_DBPrepareState->setUInt64(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setString(unsigned int parameterIndex, const std::string& value) { try { m_DBPrepareState->setString(parameterIndex,value); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } bool CMySqlDataBase::setNull(unsigned int parameterIndex, int sqlType) { try { m_DBPrepareState->setNull(parameterIndex,sqlType); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //執行命令(存儲過程) bool CMySqlDataBase::ExecuteCommand(bool bRecordset) { try { m_DBPrepareState->executeUpdate(); if(bRecordset) m_DBRecordSet.reset(m_DBPrepareState->getResultSet()); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //執行查詢(Select) bool CMySqlDataBase::Query(const std::string &szCommand) { if(szCommand.empty()) return false; //close RecordSet; CloseRecordset(); try { m_DBPrepareState.reset(m_DBConnect->prepareStatement(szCommand)); m_DBPrepareState->executeUpdate(); m_DBRecordSet.reset(m_DBPrepareState->getResultSet()); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //執行語句(Insert,Update,Delete) bool CMySqlDataBase::Execute(const std::string &szCommand) { if(szCommand.empty()) return false; //close RecordSet; CloseRecordset(); try { m_DBPrepareState.reset(m_DBConnect->prepareStatement(szCommand)); m_DBPrepareState->executeUpdate(); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取當前 Result set const ResultSet &CMySqlDataBase::GetRecordSet() { return m_DBRecordSet; } //get Next Record set bool CMySqlDataBase::GetNextResultSet() { if(m_DBPrepareState == NULL) return false; if(m_DBPrepareState->getMoreResults()) { m_DBRecordSet.reset(m_DBPrepareState->getResultSet()); return true; } return false; } //next bool CMySqlDataBase::NextFieldExist() { if(m_DBRecordSet == NULL) return false; return m_DBRecordSet->next(); } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,bool &bValue) { bValue = false; if(!IsRecordsetOpened()) return false; try { bValue = m_DBRecordSet->getBoolean(columnLabel); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,long double &dbValue) { dbValue = 0.00; if(!IsRecordsetOpened()) return false; try { dbValue = m_DBRecordSet->getDouble(columnLabel); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,int32_t &nValue) { nValue = 0; if(!IsRecordsetOpened()) return false; try { nValue = m_DBRecordSet->getInt(columnLabel); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,uint32_t &uValue) { uValue = 0; if(!IsRecordsetOpened()) return false; try { uValue = m_DBRecordSet->getUInt(columnLabel); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,int64_t &llValue) { llValue = 0; if(!IsRecordsetOpened()) return false; try { llValue = m_DBRecordSet->getInt64(columnLabel); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,uint64_t &lluValue) { lluValue = 0; if(!IsRecordsetOpened()) return false; try { lluValue = m_DBRecordSet->getUInt64(columnLabel); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,char szBuffer[],uint32_t uSize) { memset(szBuffer,0,uSize); if(!IsRecordsetOpened()) return false; try { sql::SQLString tempstr = m_DBRecordSet->getString(columnLabel); strncpy(szBuffer,tempstr.c_str(),uSize-1); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } //獲取參數 bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,std::string &szValue) { if(!IsRecordsetOpened()) return false; try { szValue = m_DBRecordSet->getString(columnLabel).asStdString(); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } /* //獲取參數,SYSTEMTIME 能夠經過 COleDateTime(const SYSTEMTIME& systimeSrc) 轉換爲 COleDateTime bool CMySqlDataBase::GetFieldValue(const std::string& columnLabel,SYSTEMTIME &systime) { if(!IsRecordsetOpened()) return false; memset(&systime,0,sizeof(SYSTEMTIME)); try { std::string timestr = m_DBRecordSet->getString(columnLabel).asStdString(); sscanf(timestr.c_str(),"%04d-%02d-%02d %02d:%02d:%02d",&systime.wYear,&systime.wMonth,&systime.wDay, &systime.wHour,&systime.wMinute,&systime.wSecond); return true; } catch (sql::SQLException &e) { SetErrorInfo(e);} return false; } */
檢查內存增加測試的 leakcheck.h:sql
#pragma once #define CRTDBG_MAP_ALLOC #include <windows.h> #include <tchar.h> #include <crtdbg.h> #include <stdlib.h> #include <iostream> #include <Psapi.h> #pragma comment(lib,"psapi.lib") #ifdef _DEBUG #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__) #define calloc(c, s) _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__) #define realloc(p, s) _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__) #define new new(_NORMAL_BLOCK,__FILE__,__LINE__) #endif #define DEFAULT_OUT_TITLE \ TEXT("缺頁中斷數 工做集(KB) 虛存(KB) 虛存峯值(KB)") #define DEFAULT_OUTPUT_FORMAT \ TEXT(" %u %u %u %u ") // 字節單位轉換,向0取整 #define B2KB(x) ((x) >> 10) /////////////////////////////////////////////////////////////////////////////////// void ConstructOutput() { PROCESS_MEMORY_COUNTERS pmc; std::cout<<DEFAULT_OUT_TITLE<<std::endl; if(!GetProcessMemoryInfo(GetCurrentProcess(),&pmc,sizeof(pmc)))return ; char output[512] = {0}; _sntprintf(output,sizeof(output),DEFAULT_OUTPUT_FORMAT, (pmc.PageFaultCount),B2KB(pmc.WorkingSetSize),B2KB(pmc.PagefileUsage),B2KB(pmc.PeakPagefileUsage)); std::cout<<output<<std::endl; }
調用 test.cppwindows
#include "MySqlDataBase.h" #include "leakcheck.h" using namespace std; int main() { CMySqlDataBase mysqldb; try{ mysqldb.SetConnectionInfo("127.0.0.1",3306,"test","root","123456"); mysqldb.OpenConnect(); ConstructOutput(); //這個函數用來查看當前內存大小的 //for(int i = 0;i<100000;++i) { mysqldb.PreparedExcute("call testproc1(?)"); mysqldb.setInt(1,1001) mysqldb.ExecuteCommand(true); while(mysqldb.NextFieldExist()) { int id; std::string name; mysqldb.GetFieldValue("id",id); mysqldb.GetFieldValue("date",name); cout<<"id:"<<id<<", name:"<<name<<endl; } } ConstructOutput(); mysqldb.CloseConnect(); } catch(CMySqlError *pSqlError) { cout<<pSqlError->getErrorCode()<<":"<<pSqlError->GetErrorDestribe()<<endl; mysqldb.CloseConnect(); } return 0; }