Solved

Writing and Reading Variables to/from BLOB fields

Posted on 1998-02-26
6
205 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

8 Experts available now in Live!

Get 1:1 Help Now