Home » Developer & Programmer » Precompilers, OCI & OCCI » NULL values in C Structures (Oracle 10g2, Solaris)
NULL values in C Structures [message #456583] Tue, 18 May 2010 10:09 Go to next message
mjm22
Messages: 54
Registered: January 2010
Location: Singapore
Member
Hi

I use a cursor to select records from a database table into a C structure as follows...

{
    int iLoop = 0;
    int iResult = 0;
    int iZeroRowCheck = 0;
    int iTotalRowsFetched = 0;
    int iTotalRowsProcessed = 0;

    EXEC SQL BEGIN DECLARE SECTION;

        int dbBatchSize = 500;

        typedef struct itemdatadb_t
        {   int             dbServiceTypeId;
            char            dbServiceId[MAX_ATTRIBUTE_LEN];
            char            dbCustRefExt[MAX_ATTRIBUTE_LEN];
            char            dbFrom[MAX_DATE_LEN];
            char            dbTo[MAX_DATE_LEN];
            float           dbMultiplier;

        } itemDataDb_t;

        itemDataDb_t *pitemDataDb;

       typedef struct ind_itemdatadb_t
        {   short   ind_dbServiceTypeId;
            short   ind_dbServiceId;
            short   ind_dbCustRefExt;
            short   ind_dbFrom;
            short   ind_dbTo;
            short   ind_dbMultiplier;

        } ind_itemDataDb_t;

        ind_itemDataDb_t *pind_itemDataDb;

    EXEC SQL END DECLARE SECTION;

    MALLOC(pitemDataDb, itemDataDb_t, gFetchBatchSize);
    MALLOC(pind_itemDataDb, ind_itemDataDb_t, gFetchBatchSize);

    EXEC SQL DECLARE curGetItems CURSOR FOR
         SELECT service_type_id,
                service_code_id,
                rsp_cust_ref_id,
                from_date,
                to_date,
                otc_multiplier
           FROM custdetails
       ORDER BY service_code_id;

    EXEC SQL OPEN curGetItems;

    for (;;)
    {
        EXEC SQL FOR :dbBatchSize
               FETCH curGetItems
                INTO :pitemDataDb:pind_itemDataDb;

        iZeroRowCheck = sqlca.sqlcode;
        iTotalRowsFetched = NUM_ROWS_FOUND;

        for (iLoop = 0; iLoop < (iTotalRowsFetched - iTotalRowsProcessed); iLoop++)
        {
            // function call here to populate an array with record retrieved

        }

        iTotalRowsProcessed = iTotalRowsFetched;

        if (iZeroRowCheck == 1403)
        {
            break;
        }
    }

    EXEC SQL CLOSE curGetItems;

    FREE(pitemDataDb);
    FREE(pind_itemDataDb);
}


The otc_multiplier field is NULL. As is the to_date sometimes. However, when I output the records later, the entries where the to_date is NULL come out fine (no value). But the otc_multiplier is getting output as 0.0 using...

// this is output later in another fuction using the following..
sprintf(newRecord, "%.1f",daServiceRecs->itemMultiplier);


If using c structures in this manner, what is the method for ensuring that numeric values are set to NULL when required?
Re: NULL values in C Structures [message #456585 is a reply to message #456583] Tue, 18 May 2010 10:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68042
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
To handle NULL values you have to first check indicator, you cannot rely on the variable value.

Regards
Michel
Re: NULL values in C Structures [message #456605 is a reply to message #456585] Tue, 18 May 2010 12:54 Go to previous message
mjm22
Messages: 54
Registered: January 2010
Location: Singapore
Member
Yeah I realise my mistake there. Thanks Michel... being a bit dumb, it's been a long day.
Previous Topic: Using Pointer to a structure for selecting multiple rows from a table
Next Topic: how to commit and rollback in external "C" procedure
Goto Forum:
  


Current Time: Mon Nov 29 16:06:57 CST 2021