Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Inserting files into Long Raw columns
jborland_at_worldbank.org (Jeff Borland) wrote:
>I'm looking for an EASY way to insert .pdf files into a table.
>Does anybody know of one?
>Jeff Borland >Worldbank >jborland_at_worldbank.org
The only ways I know are PRO C or OCI.
-> read the file in memory, set the filesize at the beginning of your
buffer -> insert into Oracle
Good luck
Thierry Melkebeke
Trisoft
Here is a sample in OCI:
int CopyFileIntoMemory (char **ppHugeBuf, char *pFilename) {
int anError = APPL_ERROR; long theFileSize = 0; char *pHugeBuf = NULL; OFSTRUCT openBuff; HFILE stream; // Open the file for reading if ((stream = OpenFile(pFilename, &openBuff, OF_READ|OF_SHARE_DENY_NONE )) == HFILE_ERROR) { vLog ("*ERR*", "unable to open file : %s",pFilename); LEAVE_FUNCTION;
}
vLog ("*ERR*", "unable to read from file : %s",pFilename); LEAVE_FUNCTION;
}
// Rewind if (_llseek(stream, 0L, 0) == HFILE_ERROR) { vLog ("*ERR*", "unable to read from file : %s",pFilename); LEAVE_FUNCTION;
}
// Checking hardware #if _DEBUG if (sizeof (long) != 4) { vLog ("*ERR*", "Oracle's header of a Blob must be 4 bytes : sizeof(long) != 4"); vExitProgram (-1);
}
// Allocate the filesize + 4 bytes
if ((pHugeBuf = (char *)malloc(theFileSize+sizeof(long))) == NULL)
{
vLog ("*ERR*", "not enough memory (needed: %ld bytes)", theFileSize+4);
LEAVE_FUNCTION;
}
*(long*)pHugeBuf = theFileSize; // Initialise the 4 first bytes with the filelength;
if (_hread (stream, pHugeBuf+sizeof(long), theFileSize) <= 0) {
vLog ("*ERR*", "unable to read from file : %s",pFilename); LEAVE_FUNCTION;
}
*ppHugeBuf = pHugeBuf; anError = OK;
ENDOF_FUNCTION:
if (stream != HFILE_ERROR) _lclose(stream);
return anError;
}
////////////////////////////////////////////////////////////////////////////int InsertPage (char *pDocName, long lDocId, char *pPageName, long SeqNo,
long PageLength, char *pRawPage)
{
int anError = OK; long lCardId = 0; sb2 ind = 0; ub2 rc = 0; ORACURSOR oc; static const char *pInsertStat = "INSERT into isource.page " "(PAGE_ID, SEQ_NO, DOCUMENT_ID, PAGE_TYPE, PAGE_LENGTH, PAGE_FILE) " "VALUES (isource.page_seq.nextval, :SEQ_NO, :DOCUMENT_ID, :FILETYPE,:PAGE_LENGTH, :PAGE_FILE)"; if (anError = oraOpenCursor(&oc))
vLog ("*ERR*", "%s", oraErrText(anError)); LEAVE_FUNCTION;
}
if (anError = oraParse(&oc, (text *)pInsertStat)) { vLog ("*ERR*", "%s", oraErrText(anError)); LEAVE_FUNCTION;
}
ORA_BIND_NBR (":DOCUMENT_ID", lDocId, &ind); ORA_BIND_NBR (":SEQ_NO", SeqNo, &ind); ORA_BIND_STR (":FILETYPE", IniFields[INI_FILE_TYPE].cValue, &ind); ORA_BIND_NBR (":PAGE_LENGTH", PageLength, &ind); ORA_BIND_RAW (":PAGE_FILE", pRawPage, &ind); switch (anError = oraExec(&oc)) { case 0: break; case UNIQUE_CONSTRAINT: // Page already exists from a previous run vLog ("*ERR*", "page [%s] already exists", pPageName); LEAVE_FUNCTION; default: vLog ("*ERR*", "%s", oraErrText(anError)); LEAVE_FUNCTION;
}
// commit after each page ! (pages are too big to accumulate in rollback segment) if ((anError = oraCommit()) != OK) { vLog ("*ERR*", "can not commit changes; %s", oraErrText(anError)); LEAVE_FUNCTION;
}
// Update ini-file for recovery (after commit) strcpy (IniFields[INI_SUBDIR].cValue, pDocName); strcpy (IniFields[INI_FILE].cValue, pPageName); if (WriteIniFile (INI_GLOBALFIELDS_ONLY) != OK) { vLog ("*ERR*", "unable to update ini-file"); LEAVE_FUNCTION;
}
ENDOF_FUNCTION: oraCloseCursor(&oc); return anError;
![]() |
![]() |