使用libpq-fe.h连接到C中的Postgres数据库时出错

问题描述 投票:-2回答:2

嘿我正在尝试使用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

谁能明白为什么会这样?

谢谢。

c postgresql libpqxx
2个回答
3
投票

你的代码编译得很好。如果我把它粘贴到x.c我可以编译它没有问题:

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

(路径可能因系统而异)。


-1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.