-2

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?

Thanks.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
user1974753
  • 1,359
  • 1
  • 18
  • 32
  • 2
    Is that code 100% identical to what you are trying to compile? Because based on these error messages I'd guess that for some reason the compiler thinks that `dbname=cs621 ` and all that follows is outside the string. Also, it complains about line 9, in the code fragment the line containing `PQconnectdb` is line 8. – fvu Apr 12 '13 at 17:20

2 Answers2

3

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).

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
-1

you may use the 64-bit libpq.lib in a 32-bit program. you can use a 32-bit libpq.lib or change you platform to x64.

a 32-bit client + 64-bit server can not work well.

exlimit
  • 1
  • 2