0
#include<stdio.h>
#include<error.h>
#include<sys/shm.h>

#include "/opt/PostgreSQL/9.1/include/libpq-fe.h"
int main()
{
   PGconn *conn;
   int buf_ptr=0;
   int i;
   {
      fprintf(stderr,"connection to database failed:%s",PQerrorMessage(conn));
      exit(0);
   }
   else
   {
      printf("connection to database successful \n");
   }
   printf("Do you want to create a table enter 1 \n ");
   scanf("%d",&i);
   if(i==1)
   {
      EXEC SQl CREATE    TABLE EMPOYE(
             ENO      INT,
             ENAME    VARCHAR(10));
   }
   return 0;
}

hello i am a newbie i am learning embedded c i want to create a simple code where a table is created in c when i am compiling the above program i am getting error like

    embc.c:25: error: âEXECâ undeclared (first use in this function)
    embc.c:25: error: (Each undeclared identifier is reported only once
    embc.c:25: error: for each function it appears in.)
    embc.c:25: error: expected â;â before âSQlâ
 please help   
devnull
  • 118,548
  • 33
  • 236
  • 227
zishan
  • 584
  • 1
  • 6
  • 12
  • 7
    As the code contains embedded SQL, it needs to be run through a special pre-processor which converts this SQL code to proper C code. As you are using PostgreSQL, you should read about [ECPG](http://www.postgresql.org/docs/9.2/static/ecpg.html). – Some programmer dude Aug 08 '13 at 07:20
  • Are you using [`ecpg`](http://www.postgresql.org/docs/9.2/static/ecpg.html) to compile? Please show the command given to compile the file. – Joachim Isaksson Aug 08 '13 at 07:22
  • 2
    Also, you are missing at least one `if()` statement - is this the actual code? – Andreas Fester Aug 08 '13 at 07:22
  • i am compiling in postgres and connection was established its just the create table that is causing error and yes andreas its the actual code – zishan Aug 08 '13 at 07:24
  • Assuming that the code were to compile, the desired SQL would not execute given the current form. [Hint: `exit 0;` and no condition.] – devnull Aug 08 '13 at 07:25
  • @zishan You're always printing `connection to database failed` and doing an exit(0), since there's no condition on running that code. – Joachim Isaksson Aug 08 '13 at 07:28
  • no no connection to database is established i commented the create table part – zishan Aug 08 '13 at 07:30
  • @zishan Then, the above is not your actual code. The above code would not even compile due to the dangling else. And, due to the embedded sql, you also need to preprocess the code, see Joachims comment – Andreas Fester Aug 08 '13 at 07:31

1 Answers1

1

First, the connection to database is missing, you should have something like :

int i=0;
EXEC SQL CONNECT TO target [AS connection-name] [USER user-name];

Or

PGconn *conn;
int buf_ptr=0;
int i=0;
conn = PQconnectdbParams(const char **keywords, const char **values, int expand_dbname);

then save your source file as prog.pgc and run :

ecpg prog.pgc

this will create a file called prog.c which can be compiled as a standard C file.

Mali
  • 2,990
  • 18
  • 18
  • yes i did make the connection now but the problem is still with the table – zishan Aug 08 '13 at 08:34
  • the code is not compilind due to some error as.c:23: error: âEXECâ undeclared (first use in this function) as.c:23: error: (Each undeclared identifier is reported only once as.c:23: error: for each function it appears in.) as.c:23: error: expected â;â before âSQLâ – zishan Aug 08 '13 at 09:15
  • I think you need to specify include path to cc : `ecpg prog.pgc` then `cc -I/opt/PostgreSQL/9.1/include -c prog.c` and `cc -o prog prog.o -L/opt/PostgreSQL/9.1/lib -lecpg` – Mali Aug 08 '13 at 09:34
  • gcc -lm -w -I/opt/PostgreSQL/9.1/include/postgresql -L/opt/PostgreSQL/9.1/lib/ -lpq as.c -o as -L/usr/lib is my compilation path – zishan Aug 08 '13 at 09:44
  • can any one please giv me a program where a table is created and inserted then i can understand where i am getting wrong – zishan Aug 08 '13 at 09:45
  • How looks your as.c after preprocessing by ecpg (specially the EXEC line statement) ? can you paste it here ? – Mali Aug 08 '13 at 09:58
  • got the issue i was not executing the command PQexec(conn,"create table emp(i int)"); thank you every one for the help :) – zishan Aug 08 '13 at 10:18
  • how to use select statement ?? i want to print what has been inserted – zishan Aug 08 '13 at 11:12