Re: How to insert data for RAW/LONG RAW type

From: Thomas J. Kyte <tkyte_at_us.oracle.com>
Date: 1996/08/13
Message-ID: <321114fb.802193_at_dcsun4>#1/1


At the bottom is an example showing INSERT and SELECT of LONG RAW data.

On 13 Aug 1996 19:44:18 GMT, Tingting Xu wrote:

>
> Hi Folks,
>
> I like to know what are the ways to insert data for RAW or
> Long RAW type column? Do we use insert?
>
> Many thanks for your time.
>
>************************************
>* Tingting Xu *
>* Software Engineer *
>* Software Artistry, Inc. *
>* Indianapolis, IN 46240 *
>* 317-574-5856 EXT.1260(O) *
>* 317-843-7477 (FAX) *
>************************************

static void process()
{

/*
 * This is the data structure we will use with our LONG RAWs.  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_raw
{

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

/*
 * Use type equivalencing to tell Oracle that the C type "my_raw" is
 * equivalent to the Oracle type LONG VARRAW and can hold upto 100k
 * 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 BEGIN DECLARE SECTION;
EXEC SQL TYPE my_raw IS LONG VARRAW(100000) REFERENCE; my_raw * buffer;
EXEC SQL END DECLARE SECTION;

long	size = 90000;
int		i;

	EXEC SQL WHENEVER SQLERROR DO sqlerror_hard();

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

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

	/*
	 * fill it up with every possible byte value from 0..254
	 */
	for( i = 0; i < size; i++ )
		buffer->arr[i] = i % 255;

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

	printf( "INSERT INTO TEST_BLOB\n" );
	EXEC SQL INSERT INTO TEST_BLOB ( 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_BLOB;\n" );
	EXEC SQL SELECT X
			 INTO :buffer
			 FROM TEST_BLOB;

	/* 
	 * 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] != i % 255 ) 
		{
			printf( "Error! %d != %d\n", buffer->arr[i], i%255 );
			break;
		}

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

}

Thomas Kyte
Oracle Government
tkyte_at_us.oracle.com                          

http://govt.us.oracle.com -- Check out our web site! Brand new, uses Oracle Web Server and Database


statements and opinions are mine and do not necessarily reflect the opinions of Oracle Corporation Received on Tue Aug 13 1996 - 00:00:00 CEST

Original text of this message