Recent

Author Topic: BufDataset query  (Read 2045 times)

HappyLarry

  • Full Member
  • ***
  • Posts: 144
BufDataset query
« on: July 31, 2016, 07:01:16 pm »
I have beentrying to use BufDataSet.
I have created a table, used Locate and LookUp and would now like to run a query such as SELECT * FROM (either SQL or other).
I can't see what to do from the BufDataSet methods.

If anyone can help I will be grateful. Below is my code so far - in case it helps.

Code: Pascal  [Select][+][-]
  1. program bufdbtest;
  2. uses
  3.   db,BufDataset;
  4.  
  5. var
  6.   BufDb:TBufDataset;
  7.   LO:TLocateOptions;
  8.  
  9.  
  10. procedure ShowTitle(Message:string);
  11. begin
  12.   writeln;
  13.   writeln(Message);
  14.   writeln('-----------------');
  15. end;
  16.  
  17. procedure ShowDataSet(B:TBufDataSet);
  18. var
  19.   k:integer;
  20. begin
  21.   //writeln(B.FieldDefs.Items[0].Name:15, B.FieldDefs.Items[1].Name:8, B.FieldDefs.Items[2].Name:8);
  22.   B.First;
  23.   For k:=1 to B.RecordCount do
  24.   begin
  25.     writeln(B.Fields[0].value:15, B.Fields[1].value:8, B.Fields[2].value:8);
  26.     B.Next;
  27.   end;
  28. end;
  29.  
  30. procedure Add(var A:TBufDataSet; f1:string; f2:integer; f3:real);
  31. begin
  32.   with A do
  33.   begin
  34.        Append;
  35.        Fields[0].Value := f1;
  36.        Fields[1].Value := f2;
  37.        Fields[2].Value := f3;
  38.   end;
  39. end;
  40.  
  41. procedure Insert(var A:TBufDataSet; f1:string; f2:integer; f3:real);
  42. begin
  43.   with A do
  44.   begin
  45.        Insert;
  46.        Fields[0].Value := f1;
  47.        Fields[1].Value := f2;
  48.        Fields[2].Value := f3;
  49.   end;
  50. end;
  51.  
  52. procedure SortBy(var A:TBufDataSet; f:string);
  53. begin
  54.   A.IndexFieldNames:=f;
  55. end;
  56.  
  57. function Find(A:TBufDataSet; f:string; V:variant):boolean;
  58. var
  59.     L:TLocateOptions;
  60. begin
  61.     L:=[];
  62.     Include(L,loPartialKey);
  63.     If A.Locate(f, v, L) then
  64.     begin
  65.       Result:=true;
  66.     end
  67.     else
  68.     begin
  69.       Result:=false;
  70.     end;
  71. end;
  72.  
  73. begin
  74.      BufDb:=TBufDataset.Create(nil);
  75.      BufDb.FieldDefs.Add('Country',ftString,20);
  76.      BufDb.FieldDefs.Add('Population',ftinteger);
  77.      BufDb.FieldDefs.Add('Area',ftFloat);
  78.      BufDB.CreateDataset;
  79.      With BufDb do
  80.      begin
  81.        Add(BufDb,'Germany',81, 357.0);
  82.        Add(BufDb,'France', 66, 675.4);
  83.        Add(BufDb,'Italy', 61, 301.2);
  84.        Add(BufDb,'UK', 65, 243.6);
  85.        Post;
  86.        SaveToFile('demo1.bds');
  87.      end;
  88.  
  89.  
  90.      ShowTitle('Table');
  91.      ShowDataSet(BufDB);
  92.  
  93.  
  94.      ShowTitle('Does Country = Germany');
  95.      LO:=[];
  96.      Include(LO,loPartialKey);
  97.      If BufDB.Locate('Country','Germany', LO) then
  98.      begin
  99.        writeln('Found');
  100.      end
  101.      else
  102.      begin
  103.        writeln('Not found');
  104.      end;
  105.  
  106.     ShowTitle('Does country = ''Ita*''?');
  107.     If Find(BufDB, 'Country','Ita') then
  108.     begin
  109.       writeln('Found Italy');
  110.     end
  111.     else
  112.     begin
  113.       writeln('Not found Italy');
  114.     end;
  115.  
  116.     ShowTitle('Does population = 81?');
  117.     If Find(BufDB, 'Population', 81) then
  118.     begin
  119.       writeln('Found 82');
  120.     end
  121.     else
  122.     begin
  123.       writeln('Not found 82');
  124.     end;
  125.  
  126.    ShowTitle('Insert');
  127.    Insert(BufDB, 'Spain', 46, 504.8);
  128.    ShowDataSet(BufDB);
  129.  
  130.    ShowTitle('If Country = Germany then get population');
  131.    writeln(BufDB.Lookup('Country','Germany','Population'));
  132.  
  133.    ShowTitle('If Population = 66 then get country');
  134.    writeln(BufDB.Lookup('Population',66,'Country'));
  135.  
  136.    ShowTitle('Sort by Country');
  137.    BufDB.IndexFieldNames:='Country';
  138.    ShowDataSet(BufDB);
  139.  
  140.  
  141.    ShowTitle('Sort by population');
  142.    SortBy(BufDB,'Population');
  143.    ShowDataSet(BufDB);
  144.  
  145.    ShowTitle('Sort by area');
  146.    SortBy(BufDB,'Area');
  147.    ShowDataSet(BufDB);
  148.    readln;
  149. end.


Use Lazarus and Free Pascal and stand on the shoulders of giants . . . very generous giants. Thank you.

wp

  • Hero Member
  • *****
  • Posts: 7378
Re: BufDataset query
« Reply #1 on: July 31, 2016, 07:38:22 pm »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018