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_strConnInfo = "";
	m_pResult_Oper = new PGResultOper;
	m_dbConn = NULL;
	m_err_str = "";
	m_bConnected = false;
}

CPostgres::~CPostgres()
{
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是在Linux系统下通过二进制文件安装PostgreSQL的步骤: 1. 首先,从PostgreSQL官网下载适用于你的Linux版本的二进制安装文件,例如:postgresql-12.5-linux-x64-binaries.tar.gz。 2. 将下载的文件解压到你想要安装PostgreSQL的目录下,例如:/usr/local/pgsql。使用以下命令: ``` sudo tar -zxvf postgresql-12.5-linux-x64-binaries.tar.gz -C /usr/local/ ``` 3. 在可执行文件中添加PostgreSQL二进制目录的路径。使用以下命令: ``` sudo su - echo 'export PATH=$PATH:/usr/local/pgsql/bin' >> /etc/profile.d/postgresql.sh exit ``` 4. 重新启动终端或使用以下命令更新环境变量: ``` source /etc/profile.d/postgresql.sh ``` 5. 创建一个新用户和组来运行PostgreSQL服务。使用以下命令: ``` sudo groupadd postgres sudo useradd -r -g postgres -s /bin/false postgres ``` 6. 创建一个新的数据目录并设置其所有者为postgres用户和组。使用以下命令: ``` sudo mkdir /usr/local/pgsql/data sudo chown -R postgres:postgres /usr/local/pgsql/data ``` 7. 初始化数据库并启动PostgreSQL服务。使用以下命令: ``` sudo -u postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start ``` 8. 设置PostgreSQL的超级用户密码。使用以下命令: ``` sudo -u postgres /usr/local/pgsql/bin/psql -U postgres template1 ``` 进入PostgreSQL终端后,输入以下命令来设置超级用户密码: ``` \password postgres ``` 然后输入新密码并确认。 现在,你已经成功地在Linux系统上安装了PostgreSQL,并启动了服务。你可以使用psql等命令行工具或pgAdmin等GUI工具来管理和操作数据库。如果你有任何问题或需要更多帮助,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值