Program with embedded sql gives compilation error

1k views Asked by At
#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   
1

There are 1 answers

8
Mali On BEST ANSWER

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.