Solved

Writing and Reading Variables to/from BLOB fields

Posted on 1998-02-26
6
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

632 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