c - 使用 C 语言连接 Postgres 数据库时出错 - 使用 libpq-fe.h

标签 c postgresql libpqxx

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

谁能明白为什么会发生这种情况吗?

谢谢。

最佳答案

你的代码编译得很好。如果我将其粘贴到 x.c 中,我可以毫无问题地编译它:

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

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

关于c - 使用 C 语言连接 Postgres 数据库时出错 - 使用 libpq-fe.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15977181/

相关文章:

c - 在 fgets() while 循环期间清空动态数组以供重复使用

c - 如何使用 jsmn 构建 C 脚本?

sql - 在 PostgreSQL 中显示正在运行的查询的全文

python - 通过 psycopg2 获取警告信息

c - 变量在一个函数中具有值,而传递给另一个函数时为 0?

c - for(++i;++i;++i) 第二个参数是 < 或 <=?

database - 在特定表空间中创建 postgres 模式

c++ - 如何在 Postgresql for C++ 中准备语句和绑定(bind)参数

c++ - pqxx 重用/重新激活工作事务

c++ - 列号 1 超出范围 0..-1