### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [Solved] SQLite Size property  (Read 326 times)

#### OmegaX

• New member
• Posts: 5
##### [Solved] SQLite Size property
« on: August 19, 2020, 01:53:43 am »
Hi, I'm loading a query with TSQLQuery. Then what I do is assigning it to a TBufDataset.

With this function:

Code: Pascal  [Select][+][-]
1. procedure CopyFromDataset(var BuffDataset: TBufDataset; DataSet: TDataSet; CopyData: Boolean);
2. Var
3.   I  : Integer;
4.   F,F1,F2 : TField;
5.   L1,L2  : TList;
6.   N : String;
7.   OriginalPosition: TBookMark;
8. begin
9.  with BuffDataset do
10.  begin
11.   Clear;
12.   Close;
13.   For I:=0 to Dataset.FieldCount-1 do
14.    begin
15.    F:=Dataset.Fields[I];
16.    if F<>nil then
17.    begin
18.          try
19.              if (F.FieldDef.DataType=ftMemo) or
20.                (F.FieldDef.DataType=ftString)then
21.                   TFieldDef.Create(FieldDefs,F.FieldName,ftString,256,F.Required,F.FieldNo)
22.               else
23.                   TFieldDef.Create(FieldDefs,F.FieldName,F.DataType,F.Size,F.Required,F.FieldNo);
24.          except
25.               TFieldDef.Create(FieldDefs,F.FieldName,F.DataType,F.Size,F.Required,F.FieldNo);
26.          end;
27.        end;
28.    end;
29.   CreateDataset;
30.   If CopyData then
31.   begin
32.     Open;
33.     L1:=TList.Create;
34.     Try
35.       L2:=TList.Create;
36.       Try
37.         For I:=0 to FieldDefs.Count-1 do
38.           begin
39.           N:=FieldDefs[I].Name;
40.           F1:=FieldByName(N);
41.           F2:=DataSet.FieldByName(N);
44.           end;
45.         DisableControls;
46.         Dataset.DisableControls;
47.         OriginalPosition:=Dataset.GetBookmark;
48.         Try
49.           Dataset.Open;
50.           Dataset.First;
51.           While not Dataset.EOF do
52.           try
53.             begin
54.                 Append;
55.                 For I:=0 to L1.Count-1 do
56.                   begin
57.                       F1:=TField(L1[i]);
58.                       F2:=TField(L2[I]);
59.                       if not F2.IsNull then
60.                       Case F1.DataType of
61.                         ftFixedChar,
62.                         ftString   : F1.AsString:=F2.AsString;
63.                         ftBoolean  : F1.AsBoolean:=F2.AsBoolean;
64.                         ftFloat    : F1.AsFloat:=F2.AsFloat;
65.                         ftLargeInt : F1.AsInteger:=F2.AsInteger;
66.                         ftSmallInt : F1.AsInteger:=F2.AsInteger;
67.                         ftInteger  : F1.AsInteger:=F2.AsInteger;
68.                         ftDate     : F1.AsDateTime:=F2.AsDateTime;
69.                         ftTime     : F1.AsDateTime:=F2.AsDateTime;
70.                         ftDateTime : F1.AsDateTime:=F2.AsDateTime;
71.                         ftMemo     : F1.AsString:=F2.AsString;
72.                         else         F1.AsString:=F2.AsString;
73.                   end
74.                   else
75.                         F1.Value:=NULL;
76.
77.               end;
78.             Try
79.               Post;
80.             except
81.               Cancel;
82.               Raise;
83.             end;
84.             Dataset.Next;
85.             end;
86.
87.           except on e:exception do
88.               N:=e.message;
89.           end;
90.         Finally
92.           Dataset.EnableControls;
93.           EnableControls;
94.         end;
95.       finally
96.         L2.Free;
97.       end;
98.     finally
99.       l1.Free;
100.     end;
101.   end;
102.  end;
103. end;

Code: Pascal  [Select][+][-]
1.                   TFieldDef.Create(FieldDefs,F.FieldName,ftString,256,F.Required,F.FieldNo)

As you can see for strings the Size is harcoded to 256. When I really want to have the size that's in the 'Length' property for each field that is a property of the field. That field limits the number of characters the string can have.

So I can read that property and set the MaxLength in the TEdit controls.

If I change the line 256 by F.Size my program stops working.

Any ideas?
« Last Edit: August 19, 2020, 03:19:01 pm by OmegaX »

#### dsiders

• Sr. Member
• Posts: 387
##### Re: SQLite Size property
« Reply #1 on: August 19, 2020, 02:35:34 am »
Lazarus 2.1 (SVN) / FPC 3.0.4 / FPC 3.2.0 / x86-win64 / Windows 8.1

#### OmegaX

• New member
• Posts: 5
##### Re: SQLite Size property
« Reply #2 on: August 19, 2020, 03:18:50 pm »
Thanks dsiders. I don't fully understand the links, but I'm solving it using PRAGMA table_info, reading the length from there and assigning it to TEdit controls.

Not touching the code I shown.

Thanks