下载地址: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/sqlite make -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,birth ses << "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()); #endif std::cout << std::endl; more = rs.moveNext(); } ses.close(); MysqL::Connector::unregisterConnector(); return 0; }
Makefile
CC = gcc CXX = g++ RM = rm -f INCLUDE=-I/usr/include/MysqL LDFLAGS = -lPocoData -lPocoFoundation -lPocoDataMysqL CFLAGS = -g -std=c++11 ALL = a a: main.o $(CXX) -o $@ $^ $(CFLAGS) $(LDFLAGS) %.o: %.cpp $(CXX) -c $< $(CFLAGS) -o $@ $(INCLUDE) clean: $(RM) $(ALL) *.o
编译过程:
[zcm@localhost poco1]$ make g++ -c main.cpp -g -std=c++11 -o main.o -I/usr/include/MysqL g++ -o a main.o -g -std=c++11 -lPocoData -lPocoFoundation -lPocoDataMysqL原文链接:https://www.f2er.com/centos/376315.html