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: ~How to use a RAW or LONG RAW column ?

Re: ~How to use a RAW or LONG RAW column ?

From: Mark Tomlinson <marktoml_at_gdi.net>
Date: Thu, 25 Jun 1998 11:35:54 GMT
Message-ID: <35933393.140760422@newshost.us.oracle.com>


Here is ONE example:

/*
** This sample Pro*C program demonstrates the simplest means of 
** manipulating long raw data in Pro*C.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 

EXEC SQL INCLUDE sqlca;

void sqlerror_hard();

typedef struct TAGlv_raw
{

	unsigned int    len; 
	unsigned char   arr[1]; 

} lvraw;

FILE* input;
FILE* output;

int fsize;

EXEC SQL BEGIN DECLARE SECTION;

	EXEC SQL TYPE lvraw IS LONG VARRAW(2048004); 
	char oracleid[50]; 
	lvraw* i_buffer;
	lvraw* o_buffer; 

EXEC SQL END DECLARE SECTION;   void main()
{

        EXEC SQL WHENEVER SQLERROR do sqlerror_hard();

        strcpy(oracleid,"scott/tiger_at_t:orlnt-5:ORCL");

	EXEC SQL CONNECT :oracleid; 
	printf("\nConnected to ORACLE as user: %s\n\n", oracleid); 
 
	input = fopen( "ifile.exe", "rb" ); 
	if ( input == NULL ) 
	{ 
		printf( "unable to open input file\n"); 
		exit(1); 
	} 
	printf( "Input file opened\n"); 

	output = fopen( "ofile.exe", "wb" ); 
	if ( output == NULL ) 
	{ 
		printf( "unable to open output file\n"); 
		exit(1); 
	} 
	printf( "Output file opened\n"); 

	fseek(input,0,SEEK_END);
	fsize = ftell(input);
	rewind(input);

	/* 
	** Under WindowsNT you could simply use the following:
	**   fsize = filelength(fileno(input));
	** by including io.h
	*/

	printf( "File length: %d\n", fsize); 

	/* 
	** I cheat here since under normal circustances the 
	** size of memory required for o_buffer would not be known
	** and would have to be stored in the DB with the BLOB.
	*/
 	i_buffer = (lvraw *)malloc( sizeof(lvraw) + fsize ); 
 	o_buffer = (lvraw *)malloc( sizeof(lvraw) + fsize ); 
	printf( "Memory allocated\n"); 

	i_buffer->len = fsize;
	o_buffer->len = i_buffer->len;

	fread( i_buffer->arr, 1, i_buffer->len, input ); 
	printf( "Inserting blob of %d bytes\n", i_buffer->len ); 
 
	EXEC SQL INSERT INTO testlraw (c1, c2) values (10, :i_buffer);

	printf( "Blob inserted\n" ); 

	EXEC SQL COMMIT WORK; 
	printf( "Blob commited\n" ); 

	printf( "About to select\n" ); 
	EXEC SQL SELECT c2 INTO :o_buffer FROM testlraw WHERE c1 = 10;

	printf( "Blob selected\n" ); 

	fwrite( o_buffer->arr, 1, o_buffer->len, output ); 

	fclose( input ); 
	fclose( output ); 
 
	EXEC SQL COMMIT WORK RELEASE; 

	exit(0); 

}

void sqlerror_hard()
{

    EXEC SQL WHENEVER SQLERROR CONTINUE;       printf("\nORACLE error detected:");     printf("\n%s \n", sqlca.sqlerrm.sqlerrmc);  

    EXEC SQL ROLLBACK WORK RELEASE;
    exit(1);
} Received on Thu Jun 25 1998 - 06:35:54 CDT

Original text of this message

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