Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> does not break out the fetch of the cursor

does not break out the fetch of the cursor

From: Wim Derweduwe <kalimero_at_ace.ulyssis.student.kuleuven.ac.be>
Date: Thu, 23 Dec 1999 09:12:48 +0100
Message-ID: <Pine.LNX.4.10.9912230906460.19663-100000@ace.ulyssis.student.kuleuven.ac.be>

The following code goes in an eternal loop when fetching the region codes. It does not break out of the loop and just keeps going on forever repeating always the last one that it teched.

Anyone any idea I putted even many breaks, to make sure it wasn't on the wrong place but it didn't help.

/*
See the header file for more information on how to use the functions and what they do.
*/

#include <stdio.h>
#include <pgsql/sqlca.h>
#include "Region.h"
#include "Database_Login.h"

/*


Declaration of CONSTANTS



*/

/*


Declaration of TYPES



*/

typedef struct Tregion

        {
        char    country[3];
        char    region[3];
        struct  Tregion *next;
        } TREGION;

typedef TREGION *TREGION_PTR;

/*


Declaration of VARIABLES



*/
TREGION_PTR             region_base=NULL;
int                     region_pro_cnt=0;

/*


Declaration of FUNCTIONS



*/

void Read_Region (Ctry, Period)

char *Ctry;
int Period;

        {
        struct  {
                char    country[3];
                char    region[3];
                } region_data;
        int     debug=1;



        TREGION_PTR     p;
        TREGION_PTR     q;

if (debug) fprintf (stderr, "Starting to read the regions from the database.\n");

        region_base = NULL;

        EXEC SQL WHENEVER SQLERROR DO sql_error ("Error during the fectch of the Region
Codes");

        EXEC SQL WHENEVER NOT FOUND DO break;
        EXEC SQL DECLARE regionscan CURSOR FOR
        SELECT country, region
        FROM BSG_station
        GROUP BY country, region
        ORDER BY country;

        EXEC SQL OPEN regionscan;
        EXEC SQL WHENEVER NOT FOUND DO break;

        for(;;)
                {
        EXEC SQL WHENEVER NOT FOUND DO break;
                EXEC SQL FETCH regionscan INTO :region_data;
        EXEC SQL WHENEVER NOT FOUND DO break;

if (debug) fprintf (stderr, "Found region.\n");

                p = (TREGION_PTR)malloc(sizeof(TREGION));
                strncpy (p->country, region_data.country, 3);
                strncpy (p->region, region_data.region, 3);
                p->next = NULL;

if (debug) fprintf (stderr, "country=|%s|-|%s|, region=|%s|-|%s|\n", region_data.country,
p->country, region_data.region, p->region);

                if (!region_base)       region_base = p;
                else q->next = p;
                q = p;
                }

        EXEC SQL CLOSE regionscan;

if (debug) fprintf (stderr, "Read the Regions and put them in the structure.\n");

        
        }
        

int Init_Region (Ctry, Period)

char *Ctry;
int Period;

        {
        int     debug=1;

if (debug) fprintf (stderr, "Starting initialise the regions.\n");

        if (!logged_in ())
                sql_login();

if (debug) fprintf (stderr, "Logged in for the Regions.\n");
        
        Read_Region (Ctry, Period);

if (debug) fprintf (stderr, "The regions are read in a structure.\n");

        return(0);
        }


int Lookup_Region (country, region)

char *country;
char *region;

        {
        TREGION_PTR    p;
        int             debug=0;

        p = region_base;
        while (p && strncmp(country,p->country,2))
        p = p->next;
        if (!p)
                {
                region = NULL;
                return(1);
                }
        strcpy (region, p->region);
        return(0);
        }               

void main ()

{
Init_Region ("BE", 200003);
}
/*

This file contains the code to login in the database. It also contains a file to handle the errors. For more information see the header file.

*/

#include <pgsql/sqlca.h>
#include <errno.h>
#include <stdio.h>
#include "Database_Login.h"

/*


Declaration of CONSTANTS



*/

/*


Declaration of TYPES



*/

/*


Declaration of VARIABLES



*/

int LOGGED_IN=0;

/*


Declaration of FUNCTIONS



*/

/*
* This function is called when an error occurs when connecting to the database
* or when reading from the database. It searches the error message matching
* the errorcode and writes it to standard out. */

void sql_error (msg)

char *msg;

        
        { 
        char            err_msg[512];
        int             buf_len, msg_len;

        EXEC SQL WHENEVER SQLERROR CONTINUE;

        fprintf(stderr,"\n%s\n", msg);

        /* Call sqlglm() to get the complete text of the error message.
*/
        buf_len = sizeof (err_msg);
        sqlglm(err_msg, &buf_len, &msg_len);
        fprintf(stderr,"%.*s\n", msg_len, err_msg);

        EXEC SQL ROLLBACK RELEASE;
        exit(1);
        } 




/*
* Function that makes the connection to the database. */

void sql_login()

        {
        char    *username;
        char    *password;
        char    *database;
        char    temp[255];
        int     debug=1;

if (debug) fprintf (stderr,"Starting to log into the database.\n");

        if (getenv("DB_LOGIN"))
                username = (char *) getenv("DB_LOGIN");
        else    
                {
                fprintf (stderr,"!!!!!\nThe environment variable DB_LOGIN
does not
exist\n!!!!!\n");
                exit(1);
                }
        
        if (getenv("DB_PASSWORD"))
                password = (char *) getenv("DB_PASSWORD");
        else
                {
                fprintf (stderr,"!!!!!\nThe environment variable
DB_PASSWORD does not
exist\n!!!!!\n");
                exit(1);
                }

        if (getenv("DB_DATABASE"))
                {
                database = (char *) getenv("DB_DATABASE");
                sprintf (temp, "Couldn't login into the database, (%s)
with username, (%s) and
password\n", database, username);
                EXEC SQL WHENEVER SQLERROR DO sql_error(temp);

                EXEC SQL CONNECT :username IDENTIFIED BY :password USING
:database;
                LOGGED_IN=1;
                }
        else
                {
                sprintf (temp, "Couldn't login into the default database
with username, (%s)
and password\nMaybe the environment variable DB_DATABASE has to be defined?\n", username);
                EXEC SQL WHENEVER SQLERROR DO sql_error(temp);
        
                EXEC SQL CONNECT :username IDENTIFIED BY :password ;

                LOGGED_IN=1;
                }

if (debug) fprintf (stderr,"Logged into the database.\n");

        }

void sql_logout()

        {

        EXEC SQL WHENEVER SQLERROR DO sql_error("Problems Login out.");
        EXEC SQL COMMIT RELEASE;
        
        LOGGED_IN=0;

        }

        

int logged_in ()

 {

        
        return (LOGGED_IN);

        }



Received on Thu Dec 23 1999 - 02:12:48 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US