精品一区二区中文在线,无遮挡h肉动漫在线观看,国产99视频精品免视看9,成全免费高清大全

poco c++框架庫應用:數(shù)據庫的連接池 -電腦資料

電腦資料 時間:2019-01-01 我要投稿
【salifelink.com - 電腦資料】

    Poco c++中的數(shù)據庫驅動部分,簡潔,干凈,工整,和數(shù)據庫連接,封裝成這樣,還是比較好用的.下面是連接MySQL連接的方法.

    一 需求說明

    與MySQL數(shù)據庫建立連接池,并在連接池中獲得一個連接,實現(xiàn)數(shù)據庫常用增刪改查

    二 目標說明

    寫出ANSI風格的代碼,并輸出高度結果到終端,驗證程序的有效性

    三 調試條件:

    1.系統(tǒng):ubuntu

    2.qt 或 其它IDE

    3.安裝了mysql,有正確的訪問賬戶和密碼

    四 例程說明

    使用IDE:Qt Creator

    項目文件:pocomysql.pro

   

QT       += core networkQT       -= guiTARGET = poco_mysqlCONFIG   += consoleCONFIG   -= app_bundleDEFINES += CHARTDIR_HIDE_OBSOLETE _CRT_SECURE_NO_WARNINGSINCLUDEPATH += /usr/local/include/Poco -I /usr/include/mysqlLIBS += -L/usr/local/lib -lPocoData -lPocoDataMySQL -lPocoDataSQLite  -lPocoCrypto   -lPocoUtil -lPocoFoundation -L /usr/lib64/mysql#LIBS += -L/usr/local/lib -lPocoData  -lPocoDataSQLite   -lPocoFoundation  -lPocoCrypto   -lPocoUtilSOURCES += \

    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;//給出訪問數(shù)據庫的信息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();        //與數(shù)據庫建立一個連接池        Poco::Data::SessionPool pool(MySQL::Connector::KEY, _dbConnString,1,32,10);        //從數(shù)據庫存連接池中獲得一個數(shù)據庫連接        Poco::Data::Session ses(pool.get());        //如果與數(shù)據庫建立會話成功,輸出連接信息        if(ses.isConnected())             std::cout << "*** Connected to " << '(' << _dbConnString << ')' << std::endl;        //如果有為名ddjj的表,先刪除,方便下面調試        ses << "DROP TABLE IF EXISTS ddjj", now;        //把查詢結果存儲到容器中        std::vector<std::string> names;        ses << "show databases",into(names), now;       //輸出查詢結果,此處列出所有數(shù)據庫名稱        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;       //實現(xiàn)數(shù)據紀錄的插入        DateTime bd(1980, 4, 1);        DateTime ld(1982, 5, 9);        ses << "INSERT INTO ddjj VALUES('Bart Simpson',  ?)", use(bd), now;        ses << "INSERT INTO ddjj VALUES('Lisa Simpson',  ?)", use(ld), now;       //實現(xiàn)查詢的方法,并輸出查詢結果        std::vector<std::string> names1;        ses << "select * from ddjj where name like 'Bart Simpson' ",            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();        //創(chuàng)建紀錄集        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

最新文章