Thread: ECPG usage

ECPG usage

From
"Jasbinder Bali"
Date:
Hi,
I have the follwing ECPG code.

-------------------------------------------------------------------------

#include <stdio.h>
EXEC SQL INCLUDE sqlca;

int main ()
{
        EXEC SQL BEGIN DECLARE SECTION;
                char movie_type[20];
                char *movie_title=NULL;
                char query_string[256];
        EXEC SQL END DECLARE SECTION;

        EXEC SQL CONNECT TO postgres;

        printf("enter the type of movie");
        scanf("%s",movie_type);

        sprintf(query_string,"SELECT title FROM films WHERE kind = '%s'",movie_type);

        printf("query string is : '%s'\n",query_string);

        EXEC SQL PREPARE s_movie FROM :query_string;
        EXEC SQL DECLARE c_movie CURSOR FOR s_movie;

        EXEC SQL OPEN c_movie;

        EXEC SQL WHENEVER NOT FOUND DO BREAK;

        while(1)
        {
                EXEC SQL FETCH IN c_movie INTO :movie_title;
              printf("%s\n",movie_title);
              movie_title = NULL;
        }

      free (movie_title);

        EXEC SQL CLOSE c_movie;

        EXEC SQL COMMIT;

        EXEC SQL DISCONNECT;

        return 0;
}

----------------------------------------------------------

At while loop, it keeps on looping forever and my table has 2 records for the query that is eventually build.
Does that mean         EXEC SQL WHENEVER NOT FOUND DO BREAK;   is not working.

Any kind of help would be appreciated.

Thanks,
~Jas

Re: ECPG usage

From
Michael Meskes
Date:
>        while(1)
>        {
>                EXEC SQL FETCH IN c_movie INTO :movie_title;
>              printf("%s\n",movie_title);
>              movie_title = NULL;
>        }
>
>      free (movie_title);

As a side note, you should free movie_title each time, not just set it
to NULL if you use auto allocation.

> At while loop, it keeps on looping forever and my table has 2 records for
> the query that is eventually build.
> Does that mean         EXEC SQL WHENEVER NOT FOUND DO BREAK;   is not
> working.

No, that would be the first time I hear about this. You could run your
program with debugging enabled (see test/*.pgc for example). Also I'd
like to know which version of ecpg you're using.

Michael
--
Michael Meskes
Email: Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
ICQ: 179140304, AIM/Yahoo: michaelmeskes, Jabber: meskes@jabber.org
Go SF 49ers! Go Rhein Fire! Use Debian GNU/Linux! Use PostgreSQL!