http://asktom.oracle.com/pls/ask/f?p=4950:8:1351055::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:945229714836,%7Blong%7D%20and%20%7Braw%7Dunee -- Thanks for the question regarding "How can I write image file on unix server from image in the oracle database?", version oracle 7.2.3
originally submitted on 23-oct-2000 4am eastern time, lasted updated 23-oct-2000 11am
--------------------------------------------------------------------------------
You AskedHello. I'm Unee NOh form south korea.
I've been suffering from finding the solutions
how to write image file on server from image in the
oracle database.I'm a beginner as part of a pro*C programming .My code is as below. Please Help me.
=======================================================
EXEC SQL CONNECT :username IDENTIFIED BY :password; if (sqlca.sqlcode < 0) {
printf("Oracle connect error!\n");
exit(1);
}
EXEC SQL CONNECT :username IDENTIFIED BY :password;EXEC SQL DECLARE emp_cur CURSOR FOR
SELECT RESNO, FACE , FACELEN , IMGTYPE FROM IMG ;
EXEC SQL OPEN emp_cur;for (;;) {
EXEC SQL WHENEVER NOT FOUND DO break;
EXEC SQL FETCH emp_cur INTO :resno, :buffer, :facelen, :imgtype; strncpy(yy , resno.arr, 2); spritf(imgfile, "%s%s%s.gif", IMGDIR, yy, resno.arr);
printf("%s",imgfile);
printf("\n ==Making image.... \n");
fp = fopen(imgfile, "w");
fputs(buffer.arr, fp);
fclose(fp);
printf("== Done! \n");break;
}========================================================
during compilation,
it occured errors in fputs.... line.I hope to recieve ur useful response.
Thanks for reading.
--------------------------------------------------------------------------------
and we said...Well, I don't know why your fputs is complaining -- hard to say since I don't
know what the error is or what buffer is defined as or anything....In short though, buffer looks like a varchar, varchars are never null terminated
so that would not work (fputs needs a null terminated string). Also, FACE is a
long raw right? If it is an image -- it could contain a NULL in it as part of
the data. You need to use fwrite, not fputs.Here is a full example. My table is:[email protected]> desc image
Name Null? Type
----------------------------- -------- --------------------
NAME NOT NULL VARCHAR2(255)
MIME_TYPE VARCHAR2(30)
IMG_SIZE NUMBER
IMAGE LONG RAWand the pro*c code that can be used to unload it is:
static void process()
{/*
This is the data structure we will use with our LONG RAWs.
We will always use pointers to this structure as
it is big and I don't want that on the stack
*/
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 2mill bytes of data
*/EXEC SQL BEGIN DECLARE SECTION;EXEC SQL TYPE my_raw IS LONG VARRAW(2000000) REFERENCE;
my_raw * buffer;varchar name[256];
varchar mime_type[256];
int img_size;EXEC SQL END DECLARE SECTION;long size = 2000000;
FILE * fp; EXEC SQL WHENEVER SQLERROR DO sqlerror_hard(); buffer = (my_raw *)malloc( size+sizeof(my_raw) ); EXEC SQL DECLARE C CURSOR FOR
select name, mime_type, img_size, image
from image; EXEC SQL OPEN C; for( ;; )
{
EXEC SQL WHENEVER NOTFOUND DO break;
EXEC SQL FETCH C INTO
:name, :mime_type, :img_size, :buffer; printf( "Name = %.*s, mime_type = %.*s, size = %d\n",
name.len, name.arr,
mime_type.len, mime_type.arr,
img_size ); /* need to NULL terminate this, I've overallocated
my varchars so I can do that
*/
name.arr[name.len] = 0; if ( (fp=fopen( name.arr, "wb" )) == NULL )
{
perror( "fopen" );
exit(1);
}
if ( fwrite( buffer->arr, 1, buffer->len, fp ) !=
buffer->len )
{
perror( "fwrite" );
exit(1);
}
fclose( fp );
}
EXEC SQL WHENEVER NOTFOUND CONTINUE; EXEC SQL CLOSE C;
free( buffer );
}
Was this response helpful to you? Let us know!
originally submitted on 23-oct-2000 4am eastern time, lasted updated 23-oct-2000 11am
--------------------------------------------------------------------------------
You AskedHello. I'm Unee NOh form south korea.
I've been suffering from finding the solutions
how to write image file on server from image in the
oracle database.I'm a beginner as part of a pro*C programming .My code is as below. Please Help me.
=======================================================
EXEC SQL CONNECT :username IDENTIFIED BY :password; if (sqlca.sqlcode < 0) {
printf("Oracle connect error!\n");
exit(1);
}
EXEC SQL CONNECT :username IDENTIFIED BY :password;EXEC SQL DECLARE emp_cur CURSOR FOR
SELECT RESNO, FACE , FACELEN , IMGTYPE FROM IMG ;
EXEC SQL OPEN emp_cur;for (;;) {
EXEC SQL WHENEVER NOT FOUND DO break;
EXEC SQL FETCH emp_cur INTO :resno, :buffer, :facelen, :imgtype; strncpy(yy , resno.arr, 2); spritf(imgfile, "%s%s%s.gif", IMGDIR, yy, resno.arr);
printf("%s",imgfile);
printf("\n ==Making image.... \n");
fp = fopen(imgfile, "w");
fputs(buffer.arr, fp);
fclose(fp);
printf("== Done! \n");break;
}========================================================
during compilation,
it occured errors in fputs.... line.I hope to recieve ur useful response.
Thanks for reading.
--------------------------------------------------------------------------------
and we said...Well, I don't know why your fputs is complaining -- hard to say since I don't
know what the error is or what buffer is defined as or anything....In short though, buffer looks like a varchar, varchars are never null terminated
so that would not work (fputs needs a null terminated string). Also, FACE is a
long raw right? If it is an image -- it could contain a NULL in it as part of
the data. You need to use fwrite, not fputs.Here is a full example. My table is:[email protected]> desc image
Name Null? Type
----------------------------- -------- --------------------
NAME NOT NULL VARCHAR2(255)
MIME_TYPE VARCHAR2(30)
IMG_SIZE NUMBER
IMAGE LONG RAWand the pro*c code that can be used to unload it is:
static void process()
{/*
This is the data structure we will use with our LONG RAWs.
We will always use pointers to this structure as
it is big and I don't want that on the stack
*/
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 2mill bytes of data
*/EXEC SQL BEGIN DECLARE SECTION;EXEC SQL TYPE my_raw IS LONG VARRAW(2000000) REFERENCE;
my_raw * buffer;varchar name[256];
varchar mime_type[256];
int img_size;EXEC SQL END DECLARE SECTION;long size = 2000000;
FILE * fp; EXEC SQL WHENEVER SQLERROR DO sqlerror_hard(); buffer = (my_raw *)malloc( size+sizeof(my_raw) ); EXEC SQL DECLARE C CURSOR FOR
select name, mime_type, img_size, image
from image; EXEC SQL OPEN C; for( ;; )
{
EXEC SQL WHENEVER NOTFOUND DO break;
EXEC SQL FETCH C INTO
:name, :mime_type, :img_size, :buffer; printf( "Name = %.*s, mime_type = %.*s, size = %d\n",
name.len, name.arr,
mime_type.len, mime_type.arr,
img_size ); /* need to NULL terminate this, I've overallocated
my varchars so I can do that
*/
name.arr[name.len] = 0; if ( (fp=fopen( name.arr, "wb" )) == NULL )
{
perror( "fopen" );
exit(1);
}
if ( fwrite( buffer->arr, 1, buffer->len, fp ) !=
buffer->len )
{
perror( "fwrite" );
exit(1);
}
fclose( fp );
}
EXEC SQL WHENEVER NOTFOUND CONTINUE; EXEC SQL CLOSE C;
free( buffer );
}
Was this response helpful to you? Let us know!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货