进一步封装poco下的mysql操作

为方便程序对mysql操作,我对poco的mysql进行了再次封装,主要是针对自己应用需要的部分。

开发工具:netbean

系统环境:centos7

poco版本: poco-1.9.0-all

主要参考poco提供的例子,可能还有部分网上内容。不过,本次封装内容也不多,大伙别笑话。

头文件

#ifndef POCOMYSQL_H
#define POCOMYSQL_H

#include <Poco/Exception.h>
#include <Poco/Format.h>
#include <Poco/Data/Session.h>
#include <Poco/Data/RecordSet.h>
#include <Poco/Data/Row.h>
#include <Poco/Tuple.h>
#include <Poco/Data/Statement.h>
#include <Poco/Data/MySQL/Utility.h>
#include <Poco/Data/MySQL/MySQL.h>
#include <Poco/Data/StatementImpl.h>
#include <Poco/Data/SessionPool.h>

#include <Poco/Data/MySQL/Connector.h>
#include <Poco/Data/MySQL/MySQLException.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
//#include <vector>
#include <list>
using std::string;
using std::wstring;

using namespace Poco::Data::Keywords;
using Poco::Data::Session;
using Poco::Data::Statement;
using Poco::Data::RecordSet;
using Poco::format;
using Poco::DateTime;
using Poco::NumberParser;
using Poco::Any;
using Poco::AnyCast;
using Poco::Int32;
using Poco::Nullable;
using Poco::Tuple;
using Poco::DynamicAny;
class PocoMySQL {
public:
    PocoMySQL();
    PocoMySQL(string host,int port,string user,string password,string db);
    PocoMySQL(string connectString);
    bool connect();
    
    PocoMySQL(const PocoMySQL& orig);
    virtual ~PocoMySQL();
    
    int execute(string sql);
    
    template<typename T>
    bool query(string sql,T &results){
        try
        {
            *_pSession << sql, into(results), now;
            return true;
        }
        catch(Poco::Exception& e){
            return false;
        }
    }
    
private:
    //Poco::SharedPtr<Poco::Data::Session> _pSession = 0;
    Session* _pSession;
    string _host;
    int _port;
    string _user;
    string _password;
    string _db;
    string _connectionstring;
    bool _connected;
};

#endif /* POCOMYSQL_H */

cpp文件:

#include "PocoMySQL.h"

PocoMySQL::PocoMySQL() {
    Poco::Data::MySQL::Connector::registerConnector();
}
PocoMySQL::PocoMySQL(string host,int port,string user,string password,string db){
    this->_host = host;
    this->_port = port;
    this->_user = user;
    this->_password = password;
    this->_db = db;
    
    //const char fmt[]="host=%s;port=%d;db=%s;user=%s;password=%s;compress=true;auto-reconnect=true";
    char* buff = new char[512];
    sprintf(buff,"host=%s;port=%d;db=%s;user=%s;password=%s;compress=true;auto-reconnect=true",this->_host.c_str(),this->_port,this->_db.c_str(),this->_user.c_str(),this->_password.c_str());
    this->_connectionstring = buff;
    delete buff;
    Poco::Data::MySQL::Connector::registerConnector();
}
PocoMySQL::PocoMySQL(string connectString){
    this->_connectionstring = connectString;
    Poco::Data::MySQL::Connector::registerConnector();
}
bool PocoMySQL::connect(){
    //Poco::Data::MySQL::Connector.registerConnector();
    
    try
    {
    //Session session(this->_connectionstring);
    _pSession = new Session(Poco::Data::MySQL::Connector::KEY, this->_connectionstring);
    std::cout << "connect to dabase " << this->_db << " success..." << std::endl;
    //this->_pSession = new Session(Poco::Data::MySQL::Connector.createSession(this->_connectionstring));
    //return this->_pSession->isConnected();
    }
    catch(Poco::Exception& e){
        std::cout << "connect to dabase " << this->_db << " fail..." << std::endl;
        return false;
    }
    return true;
}

//执行增,删,修改操作,返回影响记录的行数
int PocoMySQL::execute(string sql){
    try
    {
        Statement stt(*this->_pSession);
        stt << sql;
        size_t r = stt.execute();
        std::cout << "affected " << r << " rows" << std::endl;
        return r;
    }
    catch(Poco::Exception& e){
        std::cout << "execute " << e.displayText() << std::endl;
        return -1;
    }
    //this->_pSession << sql ,into(count),now;
}

PocoMySQL::PocoMySQL(const PocoMySQL& orig) {
}

PocoMySQL::~PocoMySQL() {
    Poco::Data::MySQL::Connector::unregisterConnector();
}

调用:

typedef Poco::Tuple<Int32, Nullable<std::string>, Nullable<Int32> > STUDENT;

int main_mysql(int argc,char * argv[]){
    PocoMySQL my("127.0.0.1",3306,"root","root","test");
    bool connect = my.connect();
    
    if(connect){
        std::cout << "connected success" << endl;
        
        string sql;
        srand((int)time(0));
        for(int i=0;i<10;i++){
            int age= rand() % 60 + 1;
            char buff[200];
            sprintf(buff,"insert into student(name,age) values('name-%d',%d)",i,age);
            sql = buff;
            my.execute(sql);
        }
        sql = "SELECT id,name,age FROM student";
        
        std::vector<STUDENT> result;
        //模板类方法的的申明与实现必须都在h文件中完成,否则链接时会报错。
        bool ret = my.query(sql,result);
        for(int i=0;i<result.size();i++){
            std::cout << result[i].get<0>() << "\t"<< result[i].get<1>() << "\t"<< result[i].get<2>() << std::endl;
        }

        //my.query3(sql);
    }else{
        std::cout << "fail" << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/jianfengcai/p/9988337.html