Linux下PostgreSQL二次C++封装libpq

Linux下安装PostgreSQL自行百度安装,废话不说,直接贴代码

     头文件: Postgres.h 

#ifndef __POSTGRES__
#define __POSTGRES__
#include <iostream>
#include <string>
#include "pqxx/pqxx"
#include <map>

using namespace std;
using namespace pqxx;

typedef std::map<int, std::map<std::string, std::string> > map_Result;
class PGResultOper;

class CPostgres
{
public:
	CPostgres();
	
	~CPostgres();

	// 建立连接 [10/21/2019 none]
	bool DBConnect(string connstring = "");

	// 断开连接 [10/21/2019 none]
	void DisDBConnect();

	// 执行增删改sql语句 [10/21/2019 none]
	bool ExecSql(const std::string& sql, bool bCommit = true);

	//执行查询sql语句 [10/21/2019 none]
	PGResultOper* Execute_Query(const std::string& sql);
private:
	// 重连 [10/21/2019 none]
	bool Is_Connect();
private:
	PGResultOper*		m_pResult_Oper;	//结果集类
	pqxx::connection*		m_dbConn; 
	pqxx::result			m_res;
	std::string				m_strConnInfo;
	std::string				m_err_str;
	bool					m_bConnected;
};

// 结果集类
class PGResultOper
{
public:
	PGResultOper();
	~PGResultOper();

public:
	// 根据字段名字取值 [10/22/2019 none]
	const char* Field(const char* col_name);

	// 根据索引号字取值 [10/22/2019 none]
	const char* Field(unsigned int col_index);

	// 根据字段名字取值 [10/22/2019 none]
	const char* Get(const char* col_name);

	// 根据索引号字段名字取值 [10/22/2019 none]
	const char* GetFieldName(unsigned int col_index);

	// 结果集大小(行数) [10/22/2019 none]
	unsigned int Row_Num();
	
	// 结果集列大小 [10/22/2019 none]
	unsigned int Column_Num();

	// 结果集行号,从1开始 [10/22/2019 none]
	unsigned int Row_Id();

	// 判断结果集是否读完 [10/22/2019 none]
	bool IsEOF();

	// 读取下一行 [10/22/2019 none]
	bool Next();

	//根据字段名字取值, 运算符重载
	const char* operator[](const char* col_name);

	// 遍历结果集操作符 [10/22/2019 none]
	bool operator++(int);

	// 存储查询结果集 [10/22/2019 none]
	void Set_map_res(const map_Result& results, const pqxx::result& res);

private:
	bool					m_bItor_first;
	map_Result::iterator	res_itor;
	map_Result				m_mapRes;
	pqxx::result			m_pq_res;
};

#endif // !__POSTGRES__

实现类:Postgres.cpp

#include "Postgres.h"

CPostgres::CPostgres()
{
	m_strConn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值