用于 PostgreSQL 的优秀 C/C++ 连接器库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1138503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:59:40  来源:igfitidea点击:

Good C/C++ connector library for PostgreSQL

c++postgresql

提问by sivabudh

My application is a commercial GIS C++ application, and I'm looking for a robust/easy to use connector for Postgresq. (Side note: I also plan to use PostGIS)

我的应用程序是一个商业 GIS C++ 应用程序,我正在寻找一个强大/易于使用的 Postgresq 连接器。(旁注:我也计划使用 PostGIS)

Does anyone have any recommendations based on your experience? A plus would be if you have tried out various ones.

有没有人根据您的经验有任何建​​议?如果您尝试过各种不同的,那就更好了。

I have looked at:

我看过:

  1. Postgres's C client
  2. pqxx
  3. QSql
  1. Postgres 的 C 客户端
  2. 知乎
  3. 查询数据库

EDITAlso, does anyone know what's a good admin GUI tool? I see a community list here. But there are so many! I'm developing on Windows, and dont mind paying for commercial tools. Someone in another Stackoverflow post suggested Maestro.

编辑另外,有谁知道什么是好的管理 GUI 工具?我在这里看到一个社区列表。但是有这么多!我在 Windows 上开发,不介意为商业工具付费。在另一个 Stackoverflow 帖子中有人建议使用Maestro

采纳答案by joe

libpq++is one provide very good connector for PostgreSQL

libpq++是一个为 PostgreSQL 提供非常好的连接器

SQLAPI++is a C++ library for accessing multiple SQL databases (Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC, SQLite).

SQLAPI++是一个 C++ 库,用于访问多个 SQL 数据库(Oracle、SQL Server、DB2、Sybase、Informix、InterBase、SQLBase、MySQL、PostgreSQL 和 ODBC、SQLite)。

Abstract Database Connectoris a C/C++ library for making connections to several databases (MySQL, mSQL, PostgreSQL, Interbase, Informix, BDE, ODBC). It runs on Linux, UNIX, BeOS, and Windows, and a dynamic driver loader for ELF OSes is under development

Abstract Database Connector是一个 C/C++ 库,用于连接多个数据库(MySQL、mSQL、PostgreSQL、Interbase、Informix、BDE、ODBC)。它在 Linux、UNIX、BeOS 和 Windows 上运行,并且正在开发 ELF 操作系统的动态驱动程序加载器

Navicatis Nice GUI tool for PostgrSQL

Navicat是用于 PostgrSQL 的不错的 GUI 工具

回答by ovanes

Take a look at SOCI. Is an open source lib under Boost Software License (one of the most non-restrictive licenses at all).

看看 SOCI。是 Boost Software License(最不受限制的许可证之一)下的开源库。

This lib is designed especially for C++ with the idea of generic programming and type safety in mind.

这个库是专为 C++ 设计的,并考虑到了泛型编程和类型安全的思想。

SOCI Site

SOCI网站

回答by Eric

I wrote a wrapper around libpq for our needs. I was a long time Zeoslib (http://sourceforge.net/projects/zeoslib/) but they were riped with problems last I used them, didn't support cached datasets and plain slow.

我为我们的需要编写了一个围绕 libpq 的包装器。我使用 Zeoslib ( http://sourceforge.net/projects/zeoslib/)已经很长时间了,但是我上次使用它们时遇到了问题,不支持缓存数据集并且速度很慢。

libpq is very, very fast.

libpq 非常非常快。

I simply download the Windows version of Postgres, copy all the DLLs to a lib directory and export the calls, include the .h and link accordingly.

我只需下载 Windows 版本的 Postgres,将所有 DLL 复制到 lib 目录并导出调用,包括 .h 和相应的链接。

I realize this is very low level but I can't emphasize enough the performance increase I'm realizing as a result.

我意识到这是非常低的水平,但我不能充分强调我因此实现的性能提升。

Our application is an accounting/ERP type business application with fairly large install base some with fairly significant concurrent many user base (60, 100 connections)... This has served us very well... You can reply back if you want more details on how we wrap libpq and handle cached updates.

我们的应用程序是一个会计/ERP 类型的业务应用程序,具有相当大的安装基础,有些具有相当大的并发用户群(60、100 个连接)...这对我们很有帮助...如果您想了解更多详细信息,可以回复关于我们如何包装 libpq 和处理缓存更新。

UPDATE:  

From a request here are the steps to wrap libpq or make use of it directly under windows.

这里的请求是包装 libpq 或直接在 Windows 下使用它的步骤。

First, to level set, I use Embarcadero RAD XE these days so the commands that follow are the command line tools that ship with RAD XE. You also need to make sure that your command line tools are in the PATH environment variable if not already. If using Visual Studio, then you will have to work out the equivalent commands. Basically I'm creating a .lib file out of a .DLL

首先,为了设置级别,我现在使用 Embarcadero RAD XE,因此后面的命令是 RAD XE 附带的命令行工具。如果还没有,您还需要确保您的命令行工具在 PATH 环境变量中。如果使用 Visual Studio,则必须计算出等效的命令。基本上我正在从 .DLL 创建一个 .lib 文件

TEST.C is a minimalist test code I wrote to make sure I understood how to use libpq and also to test my success.

TEST.C 是我编写的极简测试代码,以确保我了解如何使用 libpq 并测试我的成功。

1.  Put all the DLLs into a directory and copy the include directory.  

You do not need to install PostgreSQL using the MSI build to get these DLLs, although that would work too. I copied the DLLs from the binary build. The .H files were also taken from the binary build as well. So my directory looks like this:

您不需要使用 MSI 构建安装 PostgreSQL 来获取这些 DLL,尽管这也可以。我从二进制构建中复制了 DLL。.H 文件也取自二进制构建。所以我的目录是这样的:

  include\
     libpq-fe.h
     postgres_ext.h
  libeay32.dll
  libiconv-2.dll
  libintl-8.dll
  libpq.dll
  ssleay32.dll
  zlib1.dll



2.  Create an import library against LIBPQ.DLL as follows:
    implib -c -a libpq.lib libpq.dll

should now have a libpq.lib file in the same directory as your DLLs.

现在应该在与 DLL 相同的目录中有一个 libpq.lib 文件。

3.  build the test program (or any program) as follows:
    bcc32 test.c -l libpq.lib

test.c

测试.c

#include <stdio.h>
#include "include/libpq-fe.h"


char *db       = "mydatabasename";
char *dbserver = "hostname";
char *uname    = "username";
char *pass     = "password";

char *SQL      = "select * from public.auditlog;";
// char *SQL      = "select userid, stationid from public.auditlog";

char     buff[200];
PGconn   *dbconn;
PGresult *res;

void main(void)
{
int nFields, i, j;

  printf("Attempting to Connect to Database Server:\n");
  printf("Database: %s\n", db);
  printf("Server  : %s\n", dbserver);

  sprintf(buff, "dbname=%s host=%s port=5432 user=%s password=%s",
                 db, dbserver, uname, pass);

  dbconn = PQconnectdb(buff);

  if( PQstatus(dbconn) != CONNECTION_OK )
    printf("Connection Failed: %s\n", PQerrorMessage(dbconn) );
  else
  {
    printf("Connected Successfully!\n");

    sprintf(buff, "BEGIN; DECLARE my_portal CURSOR FOR %s", SQL);

    res = PQexec(dbconn, buff);
    if( PQresultStatus(res) != PGRES_COMMAND_OK )
    {
      printf("Error executing SQL!: %s\n", PQerrorMessage(dbconn) );
      PQclear(res);
    }
    else
    {
      PQclear(res);
      res = PQexec(dbconn, "FETCH ALL in my_portal" );

      if( PQresultStatus(res) != PGRES_TUPLES_OK )
      {
        printf("ERROR, Fetch All Failed: %s", PQerrorMessage(dbconn) );
        PQclear(res);
      }
      else
      {
        nFields = PQnfields(res);

        // Print out the field names
        for(i=0; i<nFields; i++ )
          printf("%-15s", PQfname(res, i) );

        printf("\n");

        // Print out the rows
        for(i=0; i<PQntuples(res); i++)
        {
          for(j=0; j<nFields; j++)
            printf("%-15s", PQgetvalue(res, i, j) );

          printf("\n");
        }

        res = PQexec(dbconn, "END" );
        PQclear(res);
      }
    }

  }


  PQfinish(dbconn);
}

Now to access a PostgreSQL system I simply copy the libpq.lib file into any new RAD-XE project and add the libpq.lib to the project. I have wrapped the libpq into a database transport driver that sort of separates my database access code away.

现在要访问 PostgreSQL 系统,我只需将 libpq.lib 文件复制到任何新的 RAD-XE 项目并将 libpq.lib 添加到项目中。我已将 libpq 包装到一个数据库传输驱动程序中,该驱动程序将我的数据库访问代码分开。

The following screen shot shows a RAD-XE project called ptidb that, in turn, uses libpq to provide PostgreSQL support. I also support SQLite except with SQLite I just compile the database directly.

以下屏幕截图显示了一个名为 ptidb 的 RAD-XE 项目,该项目又使用 libpq 来提供 PostgreSQL 支持。我也支持 SQLite,除了 SQLite 我只是直接编译数据库。

Then I simply ship the DLLs, listed above, along with my final product making sure the DLLs wind up in the same directory as my product.

然后我简单地将上面列出的 DLL 连同我的最终产品一起发送,以确保 DLL 与我的产品位于同一目录中。

This should get you going. If you're also interested in the C++ wrapping I do, let me know and I'll post another update with some of it.

这应该能让你继续前进。如果您也对我所做的 C++ 包装感兴趣,请告诉我,我将发布另一个更新。

enter image description here

在此处输入图片说明