Solved

Store arrays in database

Posted on 1998-01-28
4
225 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok.  Let me know if you have any more questions.

JB
0
 

Author Comment

by:hansje
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

18 Experts available now in Live!

Get 1:1 Help Now