首頁 > php教程 > PHP开发 > 主體

c++框架庫應用:資料庫的連線池

高洛峰
發布: 2016-11-02 14:45:03
原創
1990 人瀏覽過

    Poco c++中的資料庫驅動部分,簡潔,乾淨,工整,和資料庫連線,封裝成這樣,還是比較好用的.以下是連接MySQL連線的方法.

一需求說明

 池,並在連接池中獲得一個連接,實現資料庫常用增刪改查

二目標說明

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

三調試條件:

    1.系統:ubuntu

    2.qt 或其它IDE

    3.安裝了mysql,有正確的存取帳號和密碼

四程式說明

🠎po

組合

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;

information_schema

mysql

performance_schema

smart

*** tables: Bart Simpson

name

sex

:00985038

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!