0% found this document useful (0 votes)
73 views5 pages

Oracle Blob

The document discusses various ways to insert and update BLOB data in an Oracle database from Delphi. It states that BLOB data can only be inserted or edited through a table component. It also discusses trying to use a query object or stored procedure to update the BLOB, but these did not work. The code provided shows an attempt to use streams to load BLOB data from a RichEdit component into a database field.

Uploaded by

sani khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views5 pages

Oracle Blob

The document discusses various ways to insert and update BLOB data in an Oracle database from Delphi. It states that BLOB data can only be inserted or edited through a table component. It also discusses trying to use a query object or stored procedure to update the BLOB, but these did not work. The code provided shows an attempt to use streams to load BLOB data from a RichEdit component into a database field.

Uploaded by

sani khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Oracle Blob

with dm, tblMstMain do


begin

close;
Filtered := false;
FilterSQL := 'MST_ID = ' + MST_ID;
// Filter := 'MST_ID = ' + MST_ID;
Filtered := true;
Open;
Edit;

Fields.FieldByName('MST_NAME').AsString := edtMstName.Text;
Fields.FieldByName('UPDATED_DATE').AsDateTime := now;
Fields.FieldByName('UPDATED_BY').AsInteger := currUserId;

blobField := FieldByName('MST_DOC') as TBlobField;


bs := CreateBlobStream(blobField, bmWrite);

RichViewEdit1.SaveRTFToStream(bs, false);
frmReports.showMST(bs);

bs.Position := 0;
RichViewEdit1.SaveRVFToStream(bs, false);

post;

// bs.Position := 0;
// RichViewEdit1.SaveRTFToStream(bs, false);
btnSave.Cursor := crArrow;
// frmReports.showMST(bs);

end;

Oracle Blob data can only insert or Edit through Table Component
On internet insert through query object is also mention but it not working in Delphi
// tblMstMain.Open;
// tblMstMain.Edit;
// blobField := tblMstMainMST_DOC as TBlobField;
//
// bs := tblMstMain.CreateBlobStream(blobField, bmWrite);
//
//with OraCmd do
// begin
// sql.Text :=
// 'update MST_MAIN set MST_DOC = :blobVal where MST_ID = :idVal';
//
// Params.ParamByName('idVal').Value := MST_ID;
// Params.ParamByName('blobVal').LoadFromStream(bs, ftBlob);
//
//Execute;
But to execute above code, again stream object will create through DBblobField then we can load it in parameter.

There is no other effective way is mentioned. I try to do this by using stored procedure but it again dint work
OraTable1.Open;
// OraTable1.Edit;
// blobField := OraTable1MSTDOC as TBlobField;
// bs := OraTable1.CreateBlobStream(blobField, bmWrite);
//
//
// RichViewEdit1.SaveRVFToStream(bs, false);
// bs.Position := 0;
// blobField.LoadFromStream(bs);
with spUpdtDoc do
// begin
// Params.ParamByName('PMSTDOC').Value = blobField;
// // Params.ParamByName('pmstid').Value := aMstID;
//
// ExecProc;
// end;

I tried to make string through stream but it give a raw data while converting is string

-8 1 3
-7 0 -1 0 0 0 0 536870911
-9 2 0 0 2 0 1
RVStyle

//Get the data from the database as AnsiString


rtfString := sql.FieldByName('rtftext').AsAnsiString;

//Write the string into a stream


stream := TMemoryStream.Create;
stream.Clear;
stream.Write(PAnsiChar(rtfString)^, Length(rtfString));
stream.Position := 0;

//Load the stream into the RichEdit


RichEdit.PlainText := False;
RichEdit.Lines.LoadFromStream(stream);

stream.Free;

For saving the rtf text:

//Save to stream
stream := TMemoryStream.Create;
stream.Clear;

RichEdit.Lines.SaveToStream(stream);
stream.Position := 0;

//Read from the stream into an AnsiString (rtfString)


if (stream.Size > 0) then begin
SetLength(rtfString, stream.Size);
if (stream.Read(rtfString[1], stream.Size) <= 0) then
raise EStreamError.CreateFmt('End of stream reached with %d bytes left to read.',
[stream.Size]);
end;

stream.Free;

//Save to database
sql.FieldByName('rtftext').AsAnsiString := rtfString;

Tested Code (till 28 nov, 2019)


procedure TfrmUserMST.Button1Click(Sender: TObject);
var
mst_version, insMstID, aMstID: integer;
blobField: TBlobField;
bs: Tstream;
rtfString: AnsiString;
aStream2 :TStringStream;
begin
aStream2 := TStringStream.create;
RichViewEdit1.SaveHTMLToStream(aStream2, 'F:\', 'RtfToHtml','RtfToHtml',[]);
aStream2.Position := 0;

// bs := TMemoryStream.Create;
// bs.Seek(4000, soBeginning);
// bs.
// close;
// Params.ParamByName('mstID').Value := MST_ID;
// Open;
// Edit;
// if RecordCount > 0 then
// begin

// tblMstMain.Edit;

// tblMstMain.Open;
// tblMstMain.Edit;
// blobField := tblMstMainMST_DOC as TBlobField;
//
// bs := tblMstMain.CreateBlobStream(blobField, bmWrite);
//

// bs.WriteBuffer(PChar('xyz')^, Length('xyz'));
// bs.Position := 0;
// RichViewEdit1.SaveRVFToStream(bs, false);

// RichViewEdit1.Format;
// OraQuery1.Params.Clear;
// OraQuery1.Params.AddParameter.Name := 'blobVal';

// or load from stream:


// qry.Parameters.ParamByName('blobVal').LoadFromStream(MyStream, ftBlob);
// OraQuery1.Params.AddParameter.Name := 'idVal';

// OraQuery1.SQL.Text :=
// 'update MST_MAIN set MST_DOC = :blobVal where MST_ID = :idVal';
// OraQuery1.Params.ParamByName('idVal').Value := MST_ID;
// OraQuery1.Params.ParamByName('blobVal').Assign(blobField);
//
//
// OraQuery1.ExecSQL;
// with OraCmd do
// begin
// sql.Text :=
// 'update MST_MAIN set MST_DOC = :blobVal where MST_ID = :idVal';
//
// Params.ParamByName('idVal').Value := MST_ID;
// Params.ParamByName('blobVal').LoadFromStream(bs, ftBlob);
//
// Execute;
// end;
// close;
// end;
// bs.Position := 0;
with dm do
begin
// OraTable1.Open;
// OraTable1.Edit;
// blobField := OraTable1MSTDOC as TBlobField;
// bs := OraTable1.CreateBlobStream(blobField, bmWrite);
// // bs := TMemoryStream.Create;
// // bs.Clear;
// // bs.Position := 0;
// RichViewEdit1.SaveRVFToStream(bs, false);
// // bs.Position := 0;
// //
// // // Read from the stream into an AnsiString (rtfString)
// // if (bs.Size > 0) then
// // begin
// // SetLength(rtfString, bs.Size);
// // if (bs.Read(rtfString[1], bs.Size) <= 0) then
// // raise EStreamError.CreateFmt
// // ('End of stream reached with %d bytes left to read.', [bs.Size]);
// // end;
//
// ShowMessage(bs.Size.ToString);
// blobField.LoadFromStream(bs);
aMstID := insertMST(edtMstName.Text, aDemandNo, aDnID, CST_ID.ToInteger,
mst_version, MST_TEMP_ID.ToInteger, '');
with OraCmd do
begin
sql.Text := 'UPDATE PROC.MST_MAIN ' +
' SET MST_MAIN.MST_DOC = :aHTMLTEXT ' +
' WHERE MST_MAIN.MST_ID = :aMstid ';

params.ParamByName('aHTMLTEXT').Value:= aStream2.ReadString(aStream2.Size);
params.ParamByName('aMstid').Value := aMstID;
Execute;
end;
// with spUpdtDoc do
// begin
// Params.ParamByName('PMSTDOC').Value = blobField;
// // Params.ParamByName('pmstid').Value := aMstID;
//
// ExecProc;
// end;
// bs.Free;
OraTable1.close;
// with tblMstMain do
// begin
//
// Open;
// Edit;
//
// blobField := FieldByName('MST_DOC') as TBlobField;
// bs := CreateBlobStream(blobField, bmWrite);
//
// RichViewEdit1.SaveRTFToStream(bs, false);
// ShowMessage(bs.Size.ToString);
// frmReports.showMST(bs);
//
// bs.Position := 0;
// RichViewEdit1.SaveRVFToStream(bs, false);
// ShowMessage(bs.Size.ToString);
// blobField.LoadFromStream(bs);
// // post;
//
// // bs.Position := 0;
// // RichViewEdit1.SaveRTFToStream(bs, false);
// // btnSave.Cursor := crArrow;
// // frmReports.showMST(bs);
//

//
// Close;
// end;
end;
end;

You might also like