连接错误时,Postgres数据库用C - 使用的libpq-fe.h [英] Error when connecting to Postgres database in C - using libpq-fe.h
本文介绍了连接错误时,Postgres数据库用C - 使用的libpq-fe.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用的Postgres连接到数据库
Hey I am trying to connect to a database using 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;
}
和我不断收到此编译错误:
And I keep getting this compile error:
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
有人能看到为什么会这样?
Can anyone see why this is happening?
感谢。
推荐答案
您code编译就好了。如果我将其粘贴到 x.c
我可以没有任何问题编译:
Your code compiles just fine. If I paste it into x.c
I can compile it with no problems:
gcc -I /usr/pgsql-9.2/include -L /usr/pgsql-9.2/lib x.c -lpq
(路径可能您的系统上有所不同)。
(paths may differ on your system).
这篇关于连接错误时,Postgres数据库用C - 使用的libpq-fe.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文