Board index » delphi » array into tfield

array into tfield

Hallo!

Can anyone help me in storing an array[1..500]of boolean in a MS Access
database-field? (or simply in a delphi TField?)

regards, tom

 

Re:array into tfield


How do you want to store the array ? As a blob, as a string, as 500
different fields or as 500 different records ?

You can store the array in a TBlobField with:

uses
  Db;

type
  TBoolArr = array[1..500] of Boolean;

procedure WriteBoolArray(BoolArr: TBoolArr; Field: TField);
var
  S: String;
  i, j: Integer;
begin
  SetLength(S, Succ((High(BoolArr) - Low(BoolArr)) div 8));
  for i := 1 to Length(S) do
  begin
    Byte(S[i]) := 0;
    for j := 0 to 7 do
      if (Low(BoolArr) + Pred(i) * 8 + j <= High(BoolArr)) and
        BoolArr[Low(BoolArr) + Pred(i) * 8 + j] then
        Byte(S[i]) := Byte(S[i]) + (1 shl j);
  end;
  Field.AsString := S;
end;

procedure ReadBoolArray(Field: TField; BoolArr: TBoolArr);
var
  S: String;
  i, j: Integer;
begin
  S := Field.AsString;
  for i := 1 to Length(S) do
  begin
    for j := 0 to 7 do
      if Low(BoolArr) + Pred(i) * 8 + j <= High(BoolArr) then
        BoolArr[Low(BoolArr) + i * 8 + j] := (Byte(S[i]) and (1 shl j)) > 0;
  end;
end;

"Thomas Spielleitner" <thomas.spielleit...@siemens.at> schreef in bericht
news:394E17A8.537B3D14@siemens.at...

Quote
> Hallo!

> Can anyone help me in storing an array[1..500]of boolean in a MS Access
> database-field? (or simply in a delphi TField?)

> regards, tom

Re:array into tfield


Thanks for your promp answer!
Is it more (time)efficient , to store it as blob? If so, how can I do that?

"M.H. Avegaart" schrieb:

Quote
> How do you want to store the array ? As a blob, as a string, as 500
> different fields or as 500 different records ?

> You can store the array in a TBlobField with:

> uses
>   Db;

> type
>   TBoolArr = array[1..500] of Boolean;

> procedure WriteBoolArray(BoolArr: TBoolArr; Field: TField);
> var
>   S: String;
>   i, j: Integer;
> begin
>   SetLength(S, Succ((High(BoolArr) - Low(BoolArr)) div 8));
>   for i := 1 to Length(S) do
>   begin
>     Byte(S[i]) := 0;
>     for j := 0 to 7 do
>       if (Low(BoolArr) + Pred(i) * 8 + j <= High(BoolArr)) and
>         BoolArr[Low(BoolArr) + Pred(i) * 8 + j] then
>         Byte(S[i]) := Byte(S[i]) + (1 shl j);
>   end;
>   Field.AsString := S;
> end;

> procedure ReadBoolArray(Field: TField; BoolArr: TBoolArr);
> var
>   S: String;
>   i, j: Integer;
> begin
>   S := Field.AsString;
>   for i := 1 to Length(S) do
>   begin
>     for j := 0 to 7 do
>       if Low(BoolArr) + Pred(i) * 8 + j <= High(BoolArr) then
>         BoolArr[Low(BoolArr) + i * 8 + j] := (Byte(S[i]) and (1 shl j)) > 0;
>   end;
> end;

> "Thomas Spielleitner" <thomas.spielleit...@siemens.at> schreef in bericht
> news:394E17A8.537B3D14@siemens.at...
> > Hallo!

> > Can anyone help me in storing an array[1..500]of boolean in a MS Access
> > database-field? (or simply in a delphi TField?)

> > regards, tom

Re:array into tfield


The functions I suggested require a TBlobField as the Field parameter (it
must be a blob field that can hold Succ((High(BoolArr) - Low(BoolArr)) div
8) = 63 bytes).
You could store it as a string, but that would take more space (not all
binary values are allowed)...

"Thomas Spielleitner" <thomas.spielleit...@siemens.at> schreef in bericht
news:394E3199.FFFDBB3D@siemens.at...

Quote
> Thanks for your promp answer!
> Is it more (time)efficient , to store it as blob? If so, how can I do
that?

> "M.H. Avegaart" schrieb:

> > How do you want to store the array ? As a blob, as a string, as 500
> > different fields or as 500 different records ?

> > You can store the array in a TBlobField with:

> > uses
> >   Db;

> > type
> >   TBoolArr = array[1..500] of Boolean;

> > procedure WriteBoolArray(BoolArr: TBoolArr; Field: TField);
> > var
> >   S: String;
> >   i, j: Integer;
> > begin
> >   SetLength(S, Succ((High(BoolArr) - Low(BoolArr)) div 8));
> >   for i := 1 to Length(S) do
> >   begin
> >     Byte(S[i]) := 0;
> >     for j := 0 to 7 do
> >       if (Low(BoolArr) + Pred(i) * 8 + j <= High(BoolArr)) and
> >         BoolArr[Low(BoolArr) + Pred(i) * 8 + j] then
> >         Byte(S[i]) := Byte(S[i]) + (1 shl j);
> >   end;
> >   Field.AsString := S;
> > end;

> > procedure ReadBoolArray(Field: TField; BoolArr: TBoolArr);
> > var
> >   S: String;
> >   i, j: Integer;
> > begin
> >   S := Field.AsString;
> >   for i := 1 to Length(S) do
> >   begin
> >     for j := 0 to 7 do
> >       if Low(BoolArr) + Pred(i) * 8 + j <= High(BoolArr) then
> >         BoolArr[Low(BoolArr) + i * 8 + j] := (Byte(S[i]) and (1 shl j))
> 0;
> >   end;
> > end;

> > "Thomas Spielleitner" <thomas.spielleit...@siemens.at> schreef in
bericht
> > news:394E17A8.537B3D14@siemens.at...
> > > Hallo!

> > > Can anyone help me in storing an array[1..500]of boolean in a MS
Access
> > > database-field? (or simply in a delphi TField?)

> > > regards, tom

Re:array into tfield


Quote
"Thomas Spielleitner" <thomas.spielleit...@siemens.at> wrote in message

news:394E17A8.537B3D14@siemens.at...

Quote
> Hallo!

> Can anyone help me in storing an array[1..500]of boolean in a MS Access
> database-field? (or simply in a delphi TField?)

procedure StoreInField (var aVariable; varSize : Integer; aField : tField);

var    bs : tBlobStream;

begin
if aField is tBlobField
then begin
    bs := tBlobStream.Create (tBlobField (aField), bmWrite);
    try
        bs.Write (aVariable, varSize);
    finally
        bs.Free;
        end;
    end
else begin
     if aField.DataType = ftString
     then aField.AsString := String (aVariable)
     else begin
          if aField.DataSize = varSize
          then aField.SetData (pointer (@aVariable))
          else raise exception.Create ('Field ' + aField.FieldName + ' is
not the right size to store parameter');
          end;
    end;
end;

Usage:
     StoreInField (aString, 0, aStringField);
     StoreInField (anInt, SizeOf (anInt), anIntegerField);
     StoreInField (aString [1], length (aString), aMemoOrBlobField);
     StoreInField (anArray [lowBound], SizeOf (anArray), aBlobField);

Other Threads