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 -> Re: host arrays - malloc'd arrays don't work

Re: host arrays - malloc'd arrays don't work

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: Tue, 01 Feb 2000 15:56:00 -0500
Message-ID: <l9he9s4jltiluvd4m6n0rvfvj82tka9gkq@4ax.com>


A copy of this was sent to sam <roadrash_at_my-deja.com> (if that email address didn't require changing) On Tue, 01 Feb 2000 19:10:35 GMT, you wrote:

>Hello,I have been trying to get host array fetches working.All the ORCL docs
>talk about declaring the host array asarr[SOME_NUMBER],with the exception of
>pointers to arrays of structs. But withinthe struct one still must declare
>the arrays using the arraynotationHow can I get array fetches to work with
>malloc'd areas?Or is this impossible?--thanks for reading
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.

Here is one way to do it. the trick is to use the "FOR :n" on the fetch to tell us how many rows to fetch this time around.

main( argc, argv )
int argc;
char * argv[];
{
VARCHAR oracleid[50];

typedef struct {

    short len;
    char arr[30];
} oname_type;
EXEC SQL TYPE oname_type is VARCHAR( 30 );

oname_type * oname_array;

short       * oname_iarray;
int         n;
int         i;
int         last_fetched;

    strcpy( oracleid.arr, USERID );
    oracleid.len = strlen(oracleid.arr);

    EXEC SQL WHENEVER SQLERROR DO sqlerror_hard();     EXEC SQL CONNECT :oracleid;
    printf("\nConnected to ORACLE as user: %s\n\n", oracleid.arr);

    n = 100;
    oname_array = (oname_type*) malloc( n * sizeof(oname_type) );     oname_iarray = (short *) malloc( n * sizeof(short) );

    exec sql whenever sqlerror do sqlerror_hard();

    exec sql declare c1 cursor for select object_name from all_objects;

    exec sql open c1;

    exec sql for :n fetch c1 into :oname_array:oname_iarray;

    for( i = 0, last_fetched = sqlca.sqlerrd[2]; i < sqlca.sqlerrd[2]; i++ )

        printf( "%.*s\n",
                oname_iarray[i] ? strlen("(null)"):oname_array[i].len,
                oname_iarray[i] ? "(null)":oname_array[i].arr );


    printf( "\n----------------\n" );

    n = 10;

    exec sql for :n fetch c1 into :oname_array:oname_iarray;

    for( i = 0; i+last_fetched < sqlca.sqlerrd[2]; i++ )

        printf( "%.*s\n",
                oname_iarray[i] ? strlen("(null)"):oname_array[i].len,
                oname_iarray[i] ? "(null)":oname_array[i].arr );

    exec sql close c1;

    EXEC SQL COMMIT WORK RELEASE;
}

--
See http://osi.oracle.com/~tkyte/ for my columns 'Digging-in to Oracle8i'... Current article is "Part I of V, Autonomous Transactions" updated June 21'st  

Thomas Kyte                   tkyte_at_us.oracle.com
Oracle Service Industries     Reston, VA   USA

Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Tue Feb 01 2000 - 14:56:00 CST

Original text of this message

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