Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Writing and Reading Variables to/from BLOB fields

Posted on 1998-02-26
6
Medium Priority
?
215 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

971 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