next up previous contents index
Next: Embedded C (ECPG) Up: Programming Interfaces Previous: C Language Interface (LIBPQ)

  
Pgeasy (LIBPGEASY)

LIBPGEASY is a simplified C interface that hides some of the complexity of LIBPQ. Figure [*] shows a LIBPGEASY version of our state code application.  

        
        /*
         *  libpgeasy sample program
         */
         
        #include <stdio.h>
        #include <libpq-fe.h>
        #include <libpgeasy.h>                                  /* libpgeasy header file */
         
        int
        main()
        {
            char        state_code[3];                          /* holds state code entered by user */
            char        query_string[256];                      /* holds constructed SQL query */
            char        state_name[31];                         /* holds returned state name */
         
            connectdb("dbname=test");                           /* connect to the database */
         
            printf("Enter a state code:  ");                    /* prompt user for a state code */
            scanf("%2s", state_code);
         
            sprintf(query_string,                               /* create an SQL query string */
                    "SELECT name \
                     FROM statename \
                     WHERE code = '%s'", state_code);
         
            doquery(query_string);                              /* send the query */
         
            while (fetch(state_name) != END_OF_TUPLES)          /* loop through all rows returned */
                printf("%s\n", state_name);                     /* print the value returned */
         
            disconnectdb();                                     /* disconnect from the database */
         
            return 0;
        }
        
 

No error checking is required because LIBPGEASY automatically terminates the program if an error occurs. You can change this default using on_error_continue() . 


Bruce Momjian
2001-05-09