poco库开发mysql_进一步封装poco下的mysql操作

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

开发工具:netbean

系统环境:centos7

poco版本: poco-1.9.0-all

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

头文件

#ifndef POCOMYSQL_H

#define POCOMYSQL_H

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

//#include

#include

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

bool query(string sql,T &results){

try

{

*_pSession << sql, into(results), now;

return true;

}

catch(Poco::Exception& e){

return false;

}

}

private:

//Poco::SharedPtr<: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, Nullable > 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 result;

//模板类方法的的申明与实现必须都在h文件中完成,否则链接时会报错。

bool ret = my.query(sql,result);

for(int i=0;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;

}

进一步封装poco下的mysql操作

标签:cte   connector   size_t   res   std   const   cat   内容   pen

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:https://www.cnblogs.com/jianfengcai/p/9988337.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值