Poco数据库操作

1.Poco进行数据库操作的步骤一般是:

a. 创建会话(session)
b. 从DB中读写数据(into, use)
c. 使用statements
d. 使用容器(Collection) (数据,集合...)
e. 使用limit限定
f. 如何使用复杂的数据类型(如何将一个C++对象映射到数据库的表


下面是一个简单的操作数据库的示例:

#include "Poco/String.h"
#include "Poco/Format.h"
#include "Poco/Exception.h"
#include "Poco/Data/Common.h"
#include "Poco/Data/BLOB.h"
#include "Poco/Data/StatementImpl.h"
#include "Poco/Data/MySQL/Connector.h"
#include "Poco/Data/MySQL/MySQLException.h"
#include "Poco/Data/SQLite/Connector.h"
#include <iostream>

using namespace Poco::Data;
using Poco::Data::MySQL::ConnectionException;
using Poco::Data::MySQL::StatementException;
using Poco::format;
using Poco::NotFoundException;

Poco::SharedPtr<Poco::Data::Session> _pSession = 0;
//Poco::SharedPtr<SQLExecutor> _pExecutor = 0;

std::string _dbConnString = "user=smart;password=smart;db=smart;compress=true;auto-reconnect=true";

int _tmain(int argc, _TCHAR* argv[])
{
	MySQL::Connector::registerConnector();

	try
	{
		_pSession = new Session(SessionFactory::instance().create(MySQL::Connector::KEY, _dbConnString));
	}catch (ConnectionException& ex)
	{
		std::cout << "!!! WARNING: Connection failed. MySQL tests will fail !!!" << std::endl;
		std::cout << ex.displayText() << std::endl;
	}

	if (_pSession && _pSession->isConnected()) 
		std::cout << "*** Connected to " << '(' << _dbConnString << ')' << std::endl;

	int i;

	Poco::Data::Session ses(MySQL::Connector::KEY, _dbConnString);
	int count = 0;
	ses << "SELECT COUNT(*) FROM SMART_U_STREET",into(count),now;
	std::cout<<"street has "<<count<<std::endl;

	MySQL::Connector::unregisterConnector();



	//

	//SQLite::Connector::registerConnector();
	//Session ses2(SQLite::Connector::KEY,"D:\\_fengbishikaifa\\sqllite\\test_sqlite.db");
	//std::string name;
	//ses2 << "select id,name from smart_u_street",into(i),into(name),now;
	//std::cout<<""<<i<<"|"<<name<<std::endl;
	//SQLite::Connector::unregisterConnector();

	std::cin>>i;
	return 0;
}

2.Poco数据库缓冲池的使用

Poco::Data::SessionPool用于管理一组session。当需要session时,Session池首先查找已初始化的会话,如果找到了,将返回一个session对象给调用者,并设置此session对象为使用当中。

这里是使用sessionPool的一个简单的小示例。



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风水月

从心底相信自己是成功的第一步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值