Re: long datatype in Pro*C/C++

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: 1997/11/11
Message-ID: <346e7d7f.91730912_at_newshost>#1/1


On Tue, 11 Nov 1997 02:00:51 GMT, vasank_at_lips.net (Vasan K) wrote:

>
>Hi,
>
>I am trying to use a long datatype column in one of my tables. I have
>successfully inserted into the table using embedded sql in Pro*C. I
>am having trouble reading from the table. I am looking for code
>snippets that do this read of a long column from a table in Oracle 7/8
>databases.
>
>I know I can use OCI/PLSQL to do it, but I am specifically looking to
>doing it with Pro*C. Let me know if this is even possible.
>
>post responses or e-mail (preferable) to vasank_at_lips.net
>
>Thanks in advance.
>
>Vasan.

Here is an example that creates a LONG and then shows how to read it back out. Hope this help....

static void process()
{

/*
 * This is the data structure we will use with our LONGs.  We will
 * always use pointers to this structure as we don't know how big we need
 * until runtime.  Will use malloc to allocate storage on the fly.
 */
 

typedef struct TAGmy_long
{

	long			len;
	unsigned char	arr[1];
}
	my_long;

/*
 * Use type equivalencing to tell Oracle that the C type "my_long" is
 * equivalent to the Oracle type LONG VARCHAR and can hold upto 200k
 * bytes of data (we will never allocate that much here, just an upper
  • bound, could be MUCH higher (eg: 10,000,000,000) */ EXEC SQL TYPE my_long IS LONG VARCHAR(200000) REFERENCE;
my_long	* buffer;
long	size = 190000;
int		i;

	EXEC SQL WHENEVER SQLERROR DO sqlerror_hard();

	printf( "EXEC SQL CREATE TABLE TEST_LONG  ( X LONG );\n" );
	EXEC SQL CREATE TABLE TEST_LONG ( X LONG );

	/*
	 * we will allocate a little over 90k of space
	 */
	buffer = (my_long *)malloc( size+sizeof(my_long) );

	/*
	 * fill it up with every possible byte value from "A" to "Z"
	 */
	for( i = 0; i < size; i++ )
		buffer->arr[i] = 'A' + (i % 26);

	/*
	 * Just like a varchar, set the length field
	 */
	buffer->len = size;

	printf( "INSERT INTO TEST_LONG\n" );
	EXEC SQL INSERT INTO TEST_LONG ( X ) values ( :buffer );

	/*
	 * commit the changes
	 */
	printf("EXEC SQL COMMIT WORK;\n" );
	EXEC SQL COMMIT WORK;

	/*
	 * Now, ZERO out the buffer so when we fetch, we know that the buffer
	 * was NOT equal to what it was on the insert.  Just to *prove* that
	 * the fetch got back the data we inserted
	 */
	memset( buffer->arr, 0, size );

	/*
	 * Select out the data
	 */
	printf( " SELECT X INTO :buffer FROM TEST_LONG;\n" );
	EXEC SQL SELECT X
			 INTO :buffer
			 FROM TEST_LONG;

	/* 
	 * show what size we got 
	 */
	printf( "The Length = %ld\n", buffer->len );

	/*
	 * Check each character to make sure it is what we inserted
	 */
	for( i = 0; i < size; i++ )
		if ( buffer->arr[i] != 'A' + (i % 26) ) 
		{
			printf( "Error! %d != %d\n", buffer->arr[i], i%255 );
			break;
		}

	/*
	 * delete that table
	 */
	printf( "EXEC SQL DROP TABLE TEST_LONG;\n" );
	EXEC SQL DROP TABLE TEST_LONG;

}  

Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Bethesda MD  

http://govt.us.oracle.com/ -- downloadable utilities  



Opinions are mine and do not necessarily reflect those of Oracle Corporation  

Anti-Anti Spam Msg: if you want an answer emailed to you, you have to make it easy to get email to you. Any bounced email will be treated the same way i treat SPAM-- I delete it. Received on Tue Nov 11 1997 - 00:00:00 CET

Original text of this message