Solved

Store arrays in database

Posted on 1998-01-28
4
227 Views
Last Modified: 2010-08-05
I am loooking for a way to store an array (of any kind, dimension) in the database in a BLOB field (or something similar). I have tried Vararrays with TBlobFields but no succes :-(
Anyone?
0
Comment
Question by:hansje
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 200 total points
ID: 1357733
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
 

Author Comment

by:hansje
ID: 1357734
I have to try it first, but it looks Ok to me.
Many thanks!
If had any more you would get some bonuspoints for the quick reply!
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1357735
Ok.  Let me know if you have any more questions.

JB
0
 

Author Comment

by:hansje
ID: 1357736
No, thanks. I have already implemented your solution and it works just fine. Thanks again for the solution.
(will have to save up some points now before I can ask another question...)

Hans.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

777 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