centos7.2 安装poco

来源:互联网 发布:淘宝活动第三方平台 编辑:程序博客网 时间:2024/06/06 05:38

下载地址: https://pocoproject.org/releases/poco-1.7.8/poco-1.7.8p3-all.tar.gz

安装:

#!/bin/sh# 安装依赖库# yum install openssl-devel mysql++-devel# 默认方式不支持mysql#./configure --everything --omit=Data/ODBC,Data/SQLitemake -s#make -s install


具体安装时要使用什么参数,可执行 "./configure --help" 查看!


安装后,可以编写程序进行测试了。

如果程序运行时,发现找不到运行的动态库,记得将/usr/local/lib 添加到库的搜索 目录下,并执行ldconfig

---------------------------------------------------------------------------------------------------------------------------------

以下为测试程序:main.cpp

#include <iostream>#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"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=192.168.2.2;port=3306;""user=root;password=123456;""db=test;""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,birthses << "create table ddjj(name VARCHAR(20),birth 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){#if 0// 通过下标取数据for (std::size_t col = 0; col < cols; ++col){std::cout << rs[col].convert<std::string>() << "\t";}#else// 通过列名取数据printf("name=%s, birth=%s", rs["name"].convert<std::string>().c_str(), rs["birth"].convert<std::string>().c_str());#endifstd::cout << std::endl;more = rs.moveNext();}ses.close();MySQL::Connector::unregisterConnector();return 0;}

Makefile

CC = gccCXX = g++RM = rm -fINCLUDE=-I/usr/include/mysqlLDFLAGS = -lPocoData -lPocoFoundation -lPocoDataMySQLCFLAGS = -g -std=c++11ALL = aa: main.o$(CXX) -o $@ $^ $(CFLAGS) $(LDFLAGS)%.o: %.cpp$(CXX) -c $< $(CFLAGS) -o $@ $(INCLUDE)clean:$(RM) $(ALL) *.o

编译过程:

[zcm@localhost poco1]$ makeg++ -c main.cpp -g -std=c++11 -o main.o -I/usr/include/mysqlg++ -o a main.o -g -std=c++11 -lPocoData -lPocoFoundation -lPocoDataMySQL