poco c++ 是一個非常方便易用的 c++ 框架。功能非常齊全。有代碼可讀性強,只要你有不清楚的地方,你隨時可以取看源代碼。
poco c++ 交流羣:386220502
下面主要是myql 數據庫連接池的使用。
/*
* main.cpp
*
* Created on: 2015年1月22日
* Author: yuhaiyang
*/
#include "Poco/Exception.h"
#include "Poco/Data/RecordSet.h"
#include "Poco/Data/MySQL/Connector.h"
#include "Poco/Data/MySQL/MySQLException.h"
#include <Poco/Data/RecordSet.h>
#include <Poco/Tuple.h>
#include <stdint.h>
#include <vector>
#include <iostream>
using namespace Poco::Data::Keywords;
using namespace Poco;
using namespace Poco::Data;
using Poco::format;
using Poco::NotFoundException;
//====================一般步驟========================
//-----------------------註冊---------------------------
// MySQL::Connector::registerConnector();
//-------------創建 mysql Session(連接池)會話-------------------------
//Poco::Data::Session sess("MySQL",
// "host=127.0.0.1;port=3306;user=root;password=123;db=test;auto-reconnect=true");
每個參數都是在同一個字符串裏的,用 ";" 隔開。
這裏面很多參數不是必須的都有默認值,但是生成環境 推薦加上
auto-reconnect=true.
//------------------判斷是否鏈接-------------------
//if (sess.isConnected())
//------------------操作數據庫----------------------
/*
* Poco::Data::Statement select(sess); 是可以省略的。因爲我們 sess<< 時候會自動創建一個臨時的 Statement
*
*/
int main( )
{
try
{
//--------------註冊---------------------------
MySQL::Connector::registerConnector();
//------------創建會話-----------------------
Poco::Data::Session sess("MySQL",
"host=192.168.1.129 ;port=3306;user=rd;password=rd;db=rd;auto-reconnect=true");
//-----------判斷是否鏈接成功-------------
if (sess.isConnected())
{
std::cout << "yes connection is successful" << std::endl;
}
else
{
std::cout << "connection is failed" << std::endl;
}
Poco::Data::Statement sql(sess);
sql << "update users set modified=\'2014-12-30\', password=\'123456\' where username=\'yuni\'; "
//"update radcheck set value=\'123456\' where username=\'yuni\' and attribute=\'Cleartext-Password\';"
;
sql.execute();
///////下面的代碼一般也用不到的吧。有興趣的可以研究一下/////////
//--------------------------元組--------------------------
typedef Poco::Tuple< std::string ,std::string ,int> Person;
typedef std::vector<Person> People;
//-----------------------------1. 查詢-------------------
// //Poco::Data::Statement select(sess);
// //可使用元組的方式
// People data;
//
// sess << "select * from person limit 10", into(data) ,now;
// /////////////////////////uint32_t num = select.execute();//這一句的功能和 now 類似。都是立即執行語句。兩者不可並存。
//// uint32_t num = sess.execute();//返回獲取的行數
//// std::cout << "num:" << num << std::endl;
// //顯示內容
// People::const_iterator it = data.begin();
// for( ; it != data.end() ; it++ )
// {
// std::cout << it->get<0>() << " ";
// std::cout << it->get<1>() << " ";
// std::cout << it->get<2>() << std::endl;
// }
//-------------如果使用 limit() 或者 range() 那麼注意使用 select.done() 來判斷是否是全部取出數據
// People data;
// Poco::Data::Statement select( sess );
// select << "select * from person ", into(data) ,range(0,1) ,now;
//
// do{
// select.execute();
// std::cout << "one time" << std::endl;
// }while( !select.done() );
//
// People::const_iterator it = data.begin();
// for (; it != data.end(); it++)
// {
// std::cout << it->get<0>() << " ";
// std::cout << it->get<1>() << " ";
// std::cout << it->get<2>() << std::endl;
// }
//----------------------------2.異步查詢-------------------------------------
Poco::Data::Statement select( sess );
select << "select * from person", now;
Poco::Data::RecordSet rs(select);
std::size_t cols = rs.columnCount();
std::cout << cols << std::endl;
for (std::size_t col = 0; col < cols; col++)
{
std::cout << rs.columnName(col) << " ";
}
std::string name = rs.value( "name", 0) ;
std::cout << "\ndata:" << name << std::endl;
} catch (MySQL::MySQLException &e)
{
std::cerr << "MySQLException : " << e.what() << std::endl;
} catch (Poco::Exception &e)
{
std::cerr << "Exception : " << e.what() << std::endl;
} catch (...)
{
std::cerr << "some error occur" << std::endl;
}
}