windows下vs2015编译POCO及使用poco操作sqlite

1.下载POCO

https://pocoproject.org/releases/
我们选择windows下的zip包下载,注意有两个版本

  1. poco-1.9.4-all.zip
  2. poco-1.9.4.zip
    其中不带all的是基础版本,只包含基本的poco类库,带all的是全功能版本,包含sqlite,opensll等等。

2.编译Poco C++

前提条件:
1)Poco C++源码(官网下载:点击我下载);
2)已经安装VS2015;
3)已经安装cmake。

1) 进入poco-1.9.4文件夹,即源码目录,创建vsproject文件夹
2)打开windows的命令行,进入vsproject目录,执行下面命令(注意:命令最后一句的 ”. .”):

mkdir cmake_build_x86
cd cmake_build_x86
cmake -G "Visual Studio 14 2015"  ..

如果要生成64位工程,执行如下命令:

mkdir cmake_build_x64
cd cmake_build_x64
cmake -G “Visual Studio 14 2015 Win64” ..

执行上述命令之后,会在vsproject目录生成 Poco.sln工程, 使用vs2015打开,编译 POCO的动态链接库
会在vsproject目录bin, lib生成 动态库.dll,及 链接库.lib

3.测试操作SQLite

创建测试工程 ,拷贝相关头文件,dll,及lib到工程目录

测试代码如下

// test_sqlite.cpp : Defines the entry point for the console application.
//

#include 

#include "Poco/SharedPtr.h"
#include "Poco/DateTime.h"

#include "Poco/Data/SQLite/Connector.h"
#include "Poco/Data/Session.h"
#include "Poco/Data/RecordSet.h"

#pragma comment (lib , "PocoFoundationd.lib")
#pragma comment (lib , "PocoDatad.lib")
#pragma comment (lib , "PocoDataSQLited.lib")

 
using namespace Poco::Data::Keywords;
using Poco::DateTime;
using Poco::Data::Session;
using Poco::Data::Statement;
using Poco::Data::RecordSet;
using Poco::Data::SQLite::Connector;

int main(int argc, char* argv[])
{
	Connector::registerConnector();

#if 1
	// create a session
	Session session("SQLite", "sample.db");

	// drop sample table, if it exists
	session << "DROP TABLE IF EXISTS Person", now;

	// (re)create table
	session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now;

	// insert some rows
	DateTime bd(1980, 4, 1);
	DateTime ld(1982, 5, 9);
	session << "INSERT INTO Person VALUES('bob', 'beijing', 12, ?)", use(bd), now;
	session << "INSERT INTO Person VALUES('sam', 'shanghai', 10, ?)", use(ld), now;

	// a simple query
	Statement select(session);
	select << "SELECT * FROM Person";
#else
	// create a session
	Session session("SQLite", "test.db");
	Statement select(session);
	select << "SELECT * FROM user";
#endif // 0
	try
	{
		select.execute();
	}
	catch (const std::exception& e)
	{
		const char * buf = e.what();
		std::cout <<buf << std::endl;
	}

	// create a RecordSet 
	RecordSet rs(select);
	std::size_t cols = rs.columnCount();
	// print all column names
	for (std::size_t col = 0; col < cols; ++col)
	{
		std::cout << rs.columnName(col) << "\t";// << std::endl;
	}
	std::cout << std::endl;
	// iterate over all rows and columns
	bool more = rs.moveFirst();
	while (more)
	{
		for (std::size_t col = 0; col < cols; ++col)
		{
			std::cout << rs[col].convert<std::string>() << "\t";
		}
		std::cout << std::endl;
		more = rs.moveNext();
	}

	return 0;
}


工程下载: 点我下载

你可能感兴趣的:(数据库,POCO)