为方便程序对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;
}

进一步封装poco下的mysql操作的更多相关文章

  1. PHP封装的一个单例模式Mysql操作类

    掌握满足单例模式的必要条件----三私一公. ①私有的构造方法-为了防止在类外使用new关键字实例化对象. ②私有的成员属性-为了防止在类外引入这个存放对象的属性. ③私有的克隆方法-为了防止在类外通 ...

  2. centos 6x系统下源码安装mysql操作记录

    在运维工作中经常部署各种运维环境,涉及mysql数据库的安装也是时常需要的.mysql数据库安装可以选择yum在线安装,但是这种安装的mysql一般是系统自带的,版本方面可能跟需求不太匹配.可以通过源 ...

  3. CentOS7下安装Mysql和Memcached 以及 使用C#操作Mysql和Memcached

    我本身是学.net的,但是现在很多主流SQL和NOSQL都是部置在linux下,本着好学的精神,前段时间装了个虚拟机,在其装上CentOS64位的服务器系统,对于英文0基础,linux0基础的我来说, ...

  4. 在Jena框架下基于MySQL数据库实现本体的存取操作

    在Jena框架下基于MySQL数据库实现本体的存取操作 转自:http://blog.csdn.net/jtz_mpp/article/details/6224311 最近在做一个基于本体的管理系统. ...

  5. Linux下的MySQL简单操作(服务启动与关闭、启动与关闭、查看版本)

    小弟今天记录一下在Linux系统下面的MySQL的简单使用,如下: 服务启动与关闭 启动与关闭 查看版本 环境 Linux版本:centeros 6.6(下面演示),Ubuntu 12.04(参见文章 ...

  6. linux下mysql操作的命令

    最近在学习mysql,还是只菜鸟,找到下面篇文章对初学者挺有用的,所以共享下 1.linux下启动mysql的命令:   mysqladmin start /ect/init.d/mysql star ...

  7. mysql在cmd命令下执行数据库操作

    windows+r 运行cmd命令,执行以下操作! 当mysql 数据库文件相对于来说比较大的时候,这个时候你可能在正常环境下的mysql中是导入不进去的,因为mysql数据库本身就有默认的导入文件大 ...

  8. linux下的shell操作mysql

    (1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...

  9. mac下的一些mysql操作

    #一.从终端进入mysql 不同于windows下的mysql.mac下的mysql安装路径不同,所以操作上会略有不同: 以下操作以默认安装mysql为前提. ##一(1):打开终端后,先设置路径,后 ...

随机推荐

  1. jQuery WeUI

    jQuery WeUI jQuery WeUI 是专为微信公众账号开发而设计的一个简洁而强大的UI库,包含全部WeUI官方的CSS组件,并且额外提供了大量的拓展组件,丰富的组件库可以极大减少前端开发时 ...

  2. JS规则 较量较量(比较操作符) 两个操作数通过比较操作符进行比较,得到值为真(true)和假(false)。【>; <; >=; <=; !=;==】

    较量较量(比较操作符) 我们先来做道数学题,数学考试成绩中,小明考了90分,小红考了95分,问谁考的分数高? 答: 因为"95 > 90",所以小红考试成绩高. 其中大于号& ...

  3. 1、docker centos 安装

    Docker for CentOS: 第一步:使用官方yum仓库 [root@linux-node1 ~]# yum install -y yum-utils [root@linux-node1 ~] ...

  4. Atcoder arc085

    C:HSI 期望模型,不想说. #include<cstdio> using namespace std; typedef long long ll; int main() { int n ...

  5. $.ajax()方法和$.get()方法使用小结

    一. 使用JQuery的$.get()方法实现异步请求 1. 编写JSP <!DOCTYPE html> <html lang="en"> <head ...

  6. 爆表!猜猜这个大会的IQ总值有多高?

    “人人可及的未来,同样存在于「日拱一卒」的琐碎生活当中,那也是技术对生活最为直观的改变和演进.” “以通神明之德,以类万物之情”,这句来自<易经>的话,放到现今也合宜. 人类掌控事物发展的 ...

  7. 如何在 Apache Flink 中使用 Python API?

    本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink PMC,阿里巴巴高级技术专家 孙金城 分享.重点为大家介绍 Flink Python API 的现状及未来规划, ...

  8. android 头像选择以及裁剪

    一.布局申明 <ImageView android:id="@+id/head_image" android:layout_width="80dp" an ...

  9. 花园【SCOI2017期望DP入门题】

    题目描述: 小 A 的花园的长和宽分别是 L,H .小 A 喜欢在花园里做游戏.每次做游戏的时候,他都先把花园均匀分割成 L×H 个小方块,每个方块的长和宽都是 1 .然后,小 A 会从花园的西北角的 ...

  10. Java-Druid:目录

    ylbtech-Java-Druid:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://yl ...