【问题标题】:Error when connecting to Postgres database in C - using libpq-fe.h在 C 中连接到 Postgres 数据库时出错 - 使用 libpq-fe.h
【发布时间】:2013-04-12 17:12:34
【问题描述】:

嘿,我正在尝试使用 postgres 连接到数据库

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

int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621      sslmode=require user=ggales password=1234");

if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");


return 0;
}

我不断收到这个编译错误:

main.c: In function ‘main’:
main.c:9:35: warning: missing terminating " character [enabled by default]
main.c:9:2: error: missing terminating " character
main.c:10:2: error: ‘dbname’ undeclared (first use in this function)
main.c:10:2: note: each undeclared identifier is reported only once for each function it      appears in
main.c:10:9: error: ‘cs621’ undeclared (first use in this function)
main.c:10:15: error: expected ‘)’ before ‘sslmode’
main.c:10:56: warning: missing terminating " character [enabled by default]
main.c:10:15: error: missing terminating " character
main.c:16:1: error: expected ‘,’ or ‘;’ before ‘}’ token
main.c:16:1: error: expected declaration or statement at end of input

谁能明白为什么会这样?

谢谢。

【问题讨论】:

  • 该代码是否与您尝试编译的代码 100% 相同?因为基于这些错误消息,我猜出于某种原因,编译器认为dbname=cs621 以及随后的所有内容都在字符串之外。此外,它抱怨第 9 行,在代码片段中包含 PQconnectdb 的行是第 8 行。

标签: c postgresql libpqxx


【解决方案1】:

您的代码编译得很好。如果我把它粘贴到x.c 我可以毫无问题地编译它:

gcc -I /usr/pgsql-9.2/include -L /usr/pgsql-9.2/lib x.c -lpq

(您的系统上的路径可能不同)。

【讨论】:

    【解决方案2】:

    您可以在 32 位程序中使用 64 位 libpq.lib。 您可以使用 32 位 libpq.lib 或将您的平台更改为 x64。

    32 位客户端 + 64 位服务器无法正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-12
      • 2019-11-08
      • 2016-10-26
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多