poco c++フレームワークライブラリアプリケーション:データベースの接続プール
Poco c++の中のデータベース駆動部分は、簡潔で、清潔で、整っていて、データベースと接続して、このようにカプセル化して、やはり使いやすいです.以下はMySQLの接続方法です.
一需要説明
MySQLデータベースと接続プールを創立して、そして接続プールの中で1つの接続を獲得して、データベースの常用の添削を実現します
二目標説明
ANSIスタイルのコードを書き出し、高度な結果を端末に出力し、プログラムの有効性を検証する
3デバッグ条件:
1.システム:ubuntu
2.qtまたはその他のIDE
3.mysqlがインストールされており、正しいアクセスアカウントとパスワードがあります
四ルーチン説明
IDE:Qt Creatorの使用
プロジェクトファイル:pocomysql.pro
mainファイル
四出力結果
*** 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
一需要説明
MySQLデータベースと接続プールを創立して、そして接続プールの中で1つの接続を獲得して、データベースの常用の添削を実現します
二目標説明
ANSIスタイルのコードを書き出し、高度な結果を端末に出力し、プログラムの有効性を検証する
3デバッグ条件:
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 " << '(' << _dbConnString << ')' << 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('Bart Simpson', ?)", use(bd), now;
ses << "INSERT INTO ddjj VALUES('Lisa Simpson', ?)", use(ld), now;
// ,
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();
//
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