在C中连接Postgresql [英] Connect Postgresql in C

查看:285
本文介绍了在C中连接Postgresql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Windows上运行的C脚本连接到我的Postgresql数据库。

I'm trying to connect a C script running on Windows to my Postgresql database.

目前,我在网站上获得了该脚本:

For the moment, I got this script on a website :

#include <stdio.h>
#include "libpq-fe.h"
#include <string>
#include <stdlib.h>

int     main() {
PGconn          *conn;
PGresult        *res;
int             rec_count;
int             row;
int             col;



        conn = PQconnectdb("dbname=ljdata host=localhost user=dataman password=supersecret");

        if (PQstatus(conn) == CONNECTION_BAD) {
                puts("We were unable to connect to the database");
                exit(0);
        }

        res = PQexec(conn,
                "update people set phonenumber=\'5055559999\' where id=3");

        res = PQexec(conn,
                "select lastname,firstname,phonenumber from people order by id");

        if (PQresultStatus(res) != PGRES_TUPLES_OK) {
                puts("We did not get any data!");
                exit(0);
        }

        rec_count = PQntuples(res);

        printf("We received %d records.\n", rec_count);
        puts("==========================");

        for (row=0; row<rec_count; row++) {
                for (col=0; col<3; col++) {
                        printf("%s\t", PQgetvalue(res, row, col));
                }
                puts("");
        }

        puts("==========================");

        PQclear(res);

        PQfinish(conn);

        return 0;
}

我复制了libpq-fe.h,postgres_ext.c和pg_config_ext .c以避免错误。

I copied the libpq-fe.h, the postgres_ext.c and the pg_config_ext.c to avoid the errors.

现在我遇到了所有这些错误:

and now I have all this errors :


  • 未定义引用'PQconnectdb'

  • 未定义引用'PQresultStatus'

  • ...

显然,我没有包含我需要的所有功能的file.c,但找不到。

So obviously I don't have the file.c which contains all the functions I need but I can't find it.

我在另一个论坛上看到我需要创建一个makefile来告诉编译器使用libpq.dll(我有此文件),但我不知道这样做。

I saw on another forum I need to create a makefile to tell the compiler to use the libpq.dll (I have this file) but I haven't the knowledge to do that.

我该如何工作?

编辑

所以现在当我尝试像这样编译它时:

So now when I try to compile it like that :

gcc -I "C:\Program Files\PostgreSQL\9.6\include" -L "C:\Program Files\PostgreSQL\9.6\lib" test.c -lpq

我收到错误消息:

C:\Program Files\PostgreSQL\9.6\lib/libpq.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

我认为这接近最终解决方案。

I think it's near to the final solution.

从我所做的研究中,我发现libpq.dll是32位的,但我的环境是64位。这样做有什么改变吗?

From the research I made, I found that libpq.dll is 32bit, but my environnement is 64bit. do this change anything ?

编辑2

该编译现在可以正常使用这行:

The compilation now works correctly with this line :

gcc -m64 -I "C:\Program Files\PostgreSQL\9.6\include" -L "C:\Program Files\PostgreSQL\9.6\lib" test.c -lpq -o test.exe

但是当我双击.exe时,出现以下错误:

But when I double-click on the .exe I have the following error :

由于缺少LIBPQ.dll而无法启动程序

"Unable to start the program because LIBPQ.dll is missing"

因此,这显然意味着我必须将libpq.dll链接到Program Files / PG / 9.6 / lib。

So it clearly means that I have to link the libpq.dll with probably a link to Program Files/PG/9.6/lib.

这里的问题是我想构建一个独立的.exe,即使计算机目标未安装Postgresql,它也可能嵌入Postgresql库以正常工作

The problem here is that I want to build a standalone .exe which probably embed the Postgresql lib to works correctly even if the computer target haven't Postgresql installed on it

(这是可能的,例如,在Java中,当我们复制了所有外部库时构建了一个Jar时)

(This is possible, for example in Java when we build a Jar with all the external lib copied in)

在C中是否可能?我想我需要调整我的编译行,但是我没有找到可以添加的参数以将所有所需的lib导入.exe

Is it possible in C ? I think I need to adapt my compilation line but I don't found which parameter I can add to import all the needed lib in the .exe

推荐答案

从您的编辑中我发现您已经弄清楚如何使用 -I -L 现在的 gcc 选项。很好。

From your edits I see that you have figured out how to use the -I and -L options of gcc by now. Good.

我想您的麻烦在于,您正在尝试与64位 libpq.dll 链接

I guess your relaining problem is that you are trying to link with a 64-bit libpq.dll in 32-bit mode or vice versa.

您可以使用 gcc 选项<$ c $设置模式。 c> -m32 和 -m64

您可以使用以下命令检查共享库 libpq.dll文件,它应该告诉您它是32位还是64位库。

You can examine the shared library with file libpq.dll, that should tell you if it is a 32-bit or a 64-bit library.

如果 gcc 告诉您以下内容:未在其中编译64位模式,则需要安装的适当版本gcc

If gcc tells you something like: 64-bit mode not compiled in, you need to install an appropriate version of gcc.

解决运行时找不到 libpq.dll 的问题时间,请将其所在的目录放入 PATH 环境变量中,或使用选项 -Wl,-rpath,< directory使用libpq.dll>

To solve the problem that libpq.dll is not found at run time, either put the directory where it resides into the PATH environment variable, or use the option -Wl,-rpath,<directory with libpq.dll>.

这篇关于在C中连接Postgresql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆