Solved

Writing and Reading Variables to/from BLOB fields

Posted on 1998-02-26
6
206 Views
Last Modified: 2010-04-06
I have a record variable I am trying to write to and read from a BLOB field.

How do I go about this?

I have tried using BlobStreams but without much success.
The examples in the Help file will not even compile.

Thanks

Chris
0
Comment
Question by:ChrisBerry
6 Comments
 
LVL 2

Expert Comment

by:Waldek
ID: 1359821
Do You create a Stream before save to stream ?
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1359822
Hi

Here is my answer from a previous similar question.



Here is code to save an array, record, structure, etc. to a blob field, and read it back in.  In my e.g. I use an array of integer, but it could be Delphi type (i.e. record) etc.

procedure SaveToBlob;
var
  i: Integer;
  tbl: TTable;
  bf: TBlobField;
  arr: array[1..20] of Integer;
  MemStr: TMemoryStream;
begin
  // Fill array with values.
  FillChar(arr, SizeOf(arr), 0);
  for i := 1 to 20 do
    arr[i] := i * 5;

  // Create a memory stream, and copy array into it.
  MemStr := TMemoryStream.Create;
  MemStr.SetSize(SizeOf(arr));
  CopyMemory(MemStr.Memory, @arr, SizeOf(arr));

  // Open table, save contents of memory stream to blob field.
  tbl := TTable.Create(Self);
  try
    tbl.DatabaseName := 'C:\Temp';
    tbl.TableName := 'Test.db';
    tbl.Active := True;
    tbl.Append;
      bf := TBlobField(tbl.FieldByName('MyBlob'));
      bf.LoadFromStream(MemStr);
    tbl.Post;
  finally
    tbl.Free;
  end;

  MemStr.Free;
end;

procedure LoadFromBlob;
var
  i: Integer;
  tbl: TTable;
  bf: TBlobField;
  arr: array[1..20] of Integer;
  MemStr: TMemoryStream;
begin
  // Create memory stream.
  MemStr := TMemoryStream.Create;
  MemStr.SetSize(SizeOf(arr));

  // Open table, move to last record, get content of blob field into memory stream.
  tbl := TTable.Create(Self);
  try
    tbl.DatabaseName := 'C:\Temp';
    tbl.TableName := 'Test.db';
    tbl.Active := True;
    tbl.Last;
    bf := TBlobField(tbl.FieldByName('MyBlob'));
    bf.SaveToStream(MemStr);
  finally
    tbl.Free;
  end;

  // Copy content of memory stream to arr.
  CopyMemory(@arr, MemStr.Memory, SizeOf(arr));
  MemStr.Free;
  // arr now contains the data from blob field...
end;

Regards,
JB
0
 
LVL 5

Accepted Solution

by:
inter earned 50 total points
ID: 1359823
Assume you have the following record:

type
  DullRecord = record
     x : integer;
     y : real;
     z : array[0..200] of char;
  end;

and you have created a database with the field called Blob1. The in you program you should create a field coresponding to it and name it as MyBlob(just double click on the table and add the field, rename it or not);

Assuming that your table is open, and current record is set, the following code writes the record to your blob field:(assume you table name is Table1):

procedure Form1.WriteToBlob(var R : TDullRecord);
var
  S : TMemoryStream; //besure to include the coresponding unit
begin
  // Create the memory stream
  S := TMemoryStream.Create;
  try
    //Put table to edit state
    Table1.Edit;
    //write record to memory
    S.Write(R, Sizeof(R));
    //read it to blob field
    MyBlob.LoadFromStream(S);
    //make changes permenant
    Table1.Post;
  finally
    S.Free;
  end;
end;

Ask anything, I am here,
Sincerely
Igor
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ChrisBerry
ID: 1359824
Thanks one and all, the fastest replies ever.

The problem I had was that the Borland example uses TBlobStream := CreateBlobStream which returns TStream and there for incompatible.

Chris

0
 
LVL 5

Expert Comment

by:inter
ID: 1359825
JimBob excuse me,

Yyour coment is not there when I enter the page, I just cut and paste, (tell me if I could do anything for you-although you are our superstar-my special subject is image processing, optimization and computer vision)sorry again.

Igor
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1359826
Hi Igor

No problem.  It's happened to me before.  ;-)

Cheers,
JB
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now