Solved

Writing and Reading Variables to/from BLOB fields

Posted on 1998-02-26
6
209 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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