搜索
首页php教程PHP开发c++框架库应用:数据库的连接池

    Poco c++中的数据库驱动部分,简洁,干净,工整,和数据库连接,封装成这样,还是比较好用的.下面是连接MySQL连接的方法.

一 需求说明

    与MySQL数据库建立连接池,并在连接池中获得一个连接,实现数据库常用增删改查

二 目标说明

    写出ANSI风格的代码,并输出高度结果到终端,验证程序的有效性

三 调试条件:

    1.系统:ubuntu

    2.qt 或 其它IDE

    3.安装了mysql,有正确的访问账户和密码

四 例程说明

    使用IDE:Qt Creator

    项目文件:pocomysql.pro

QT       += core network
QT       -= gui
TARGET = poco_mysql
CONFIG   += console
CONFIG   -= app_bundle
 
DEFINES += CHARTDIR_HIDE_OBSOLETE _CRT_SECURE_NO_WARNINGS
INCLUDEPATH += /usr/local/include/Poco -I /usr/include/mysql
LIBS += -L/usr/local/lib -lPocoData -lPocoDataMySQL -lPocoDataSQLite  -lPocoCrypto   -lPocoUtil -lPocoFoundation -L /usr/lib64/mysql
#LIBS += -L/usr/local/lib -lPocoData  -lPocoDataSQLite   -lPocoFoundation  -lPocoCrypto   -lPocoUtil
SOURCES += \
    mysql.cpp

main文件

#include "Poco/String.h"
#include "Poco/Format.h"
#include "Poco/Exception.h"
#include "Poco/Data/StatementImpl.h"
#include "Poco/Data/MySQL/Connector.h"
#include "Poco/Data/MySQL/MySQLException.h"
#include "Poco/Data/Session.h"
#include "Poco/Data/SessionPool.h"
#include "Poco/Data/SessionFactory.h"
#include "Poco/Data/LOB.h"
#include "Poco/Data/MySQL/MySQLStatementImpl.h"
#include "Poco/DateTime.h"
#include "Poco/Data/RecordSet.h"
#include "Poco/Data/Column.h"
 
#include <iostream>
 
using namespace Poco::Data::Keywords;
using namespace Poco::Data;
using Poco::Data::Session;
using Poco::Data::MySQL::ConnectionException;
using Poco::Data::MySQL::StatementException;
using Poco::NotFoundException;
using Poco::Data::Statement;
using Poco::DateTime;
using Poco::Data::RecordSet;
//给出访问数据库的信息
std::string _dbConnString  = "host=localhost;port=3306;"
                                                "user=root;password=19810311;"
                                                "db=smart;"
                                                "compress=true;auto-reconnect=true";
 
int main(int argc, char** argv)
{
        MySQL::Connector::registerConnector();
        //与数据库建立一个连接池
        Poco::Data::SessionPool pool(MySQL::Connector::KEY, _dbConnString,1,32,10);
        //从数据库存连接池中获得一个数据库连接
        Poco::Data::Session ses(pool.get());
        //如果与数据库建立会话成功,输出连接信息
        if(ses.isConnected())
             std::cout << "*** Connected to " << &#39;(&#39; << _dbConnString << &#39;)&#39; << std::endl;
        //如果有为名ddjj的表,先删除,方便下面调试
        ses << "DROP TABLE IF EXISTS ddjj", now;
 
        //把查询结果存储到容器中
        std::vector<std::string> names;
        ses << "show databases",into(names), now;
       //输出查询结果,此处列出所有数据库名称
        for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
        {
            std::cout << *it << std::endl;
        }
 
        // 建立一个表,名为ddjj,字段名:name,sex
        ses << "create table ddjj(name VARCHAR(20),sex VARCHAR(20));", now;
       //实现数据纪录的插入
        DateTime bd(1980, 4, 1);
        DateTime ld(1982, 5, 9);
        ses << "INSERT INTO ddjj VALUES(&#39;Bart Simpson&#39;,  ?)", use(bd), now;
        ses << "INSERT INTO ddjj VALUES(&#39;Lisa Simpson&#39;,  ?)", use(ld), now;
       //实现查询的方法,并输出查询结果
        std::vector<std::string> names1;
        ses << "select * from ddjj where name like &#39;Bart Simpson&#39; ",
            into(names1),
            now;
        for (std::vector<std::string>::const_iterator it = names1.begin(); it != names1.end(); ++it)
        {
           std::cout << "*** tables: " << *it << std::endl;
        }
 
        Statement select(ses);
        select << "SELECT * FROM ddjj";
        select.execute();
 
        //创建纪录集
        RecordSet rs(select);
        std::size_t cols = rs.columnCount();
        //输出列名
        for (std::size_t col = 0; col < cols; ++col)
        {
            std::cout << rs.columnName(col) << std::endl;
        }
        //输出所有查询到的结果
        bool more = rs.moveFirst();
        while (more)
        {
            for (std::size_t col = 0; col < cols; ++col)
            {
                std::cout << rs[col].convert<std::string>() << " ";
            }
            std::cout << std::endl;
            more = rs.moveNext();
        }
 
        ses.close();
        MySQL::Connector::unregisterConnector();
        return 0;
}

四 输出结果

*** Connected to (host=localhost;port=3306;user=root;password=19810311;db=smart;compress=true;auto-reconnect=true)

information_schema

mysql

performance_schema

smart

*** tables: Bart Simpson

name

sex

Bart Simpson 1980-04-01 00:00:00 

Lisa Simpson 1982-05-09 00:00:00 


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),