Recent

Author Topic: TBufDataSet index  (Read 7725 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
TBufDataSet index
« on: December 31, 2013, 02:49:17 pm »
Hello guys, I have a problem. Who tells me how to create indexes for the data contained in a TBufDataSet? For example, how do I change the following code to index both columns?


Code: [Select]
     VDSet_Chiavi.FieldDefs.Add('Id',ftInteger);
     VDSet_Chiavi.FieldDefs.Add('Chiave',ftString,50);
     VDSet_Chiavi.MaxIndexesCount:=10;

     VDSet_Chiavi.CreateDataset;{creo il dataset virtuale con la struttura sopra dichiarata}
     VDSet_Chiavi.Open; {apro la connessione al dataset virtuale}
     VDSet_Chiavi.Append; {dico che voglio accodare dei dati al dataset virtuale}
     VDSet_Chiavi.Fields[0].Value:=1;
     VDSet_Chiavi.Fields[1].Value:='value 1';
     VDSet_Chiavi.Post; {applico l'accodamento}
     VDSet_Chiavi.Append;{dico che voglio accodare dei dati al dataset virtuale}
     VDSet_Chiavi.Fields[0].Value:=2;
     VDSet_Chiavi.Fields[1].Value:='value 2';
     VDSet_Chiavi.Post; {applico l'accodamento}
     VDSet_Chiavi.Append;{dico che voglio accodare dei dati al dataset virtuale}
     VDSet_Chiavi.Fields[0].Value:=3;
     VDSet_Chiavi.Fields[1].Value:='value 3';
     VDSet_Chiavi.Post; {applico l'accodamento}
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TBufDataSet index
« Reply #1 on: January 01, 2014, 03:05:00 am »
The small project attached gives you example code for indexing and filtering two related TBufDatasets.

By "index both columns" do you mean you want two indexes, one on each field, or an expression index that combines the two fields?

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: TBufDataSet index
« Reply #2 on: January 01, 2014, 11:11:23 am »
Thank you :)

 expression index that combines the two fields

They also do not understand why at the start of my program which I am attaching does not auto populate the field NewLookUpField. You know tell me why, would he do if the fields were indexed.

Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TBufDataSet index
« Reply #3 on: January 01, 2014, 12:09:54 pm »
Here's an example of setting an expression index. You'll have to place a TBufDataset, datasource, and dbgrid on your form and hook them up appropriately, then add OnCreate and OnDestroy handlers for your form as follows. Of course in a proper program your datasets would be put in a separate datamodule.

Code: [Select]
unit mainExpressionIdx;

{$mode objfpc}{$H+}

interface

uses
  Forms, DBGrids, DbCtrls, db, BufDataset, Classes;

type

  { TForm1 }

  TForm1 = class(TForm)
    dataNames: TBufDataset;
    dsNames: TDatasource;
    grdNames: TDBGrid;
    navNames: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  dataNames.FieldDefs.Add('Forename',ftString,15,True);
  dataNames.FieldDefs.Add('Surname',ftString,15,True);
  dataNames.FieldDefs.Add('ID',ftAutoInc,0,True);
  dataNames.CreateDataset;
  dataNames.IndexDefs.Add('ForenameSurnameExprIdx','Surname;Forename',[ixCaseInsensitive,ixUnique]);
  dataNames.Open;
  dataNames.AppendRecord(['Arthur','Gates',0]);
  dataNames.AppendRecord(['Arthur','Jones',0]);
  dataNames.AppendRecord(['Bill','Gates',0]);
  dataNames.AppendRecord(['Emily','Gates',0]);
  dataNames.AppendRecord(['Tim','Avery',0]);
  dataNames.AppendRecord(['Sven','Obama',0]);
  dataNames.IndexFieldNames:='Surname;Forename';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  dataNames.Close;
end;

end.


dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: TBufDataSet index
« Reply #4 on: December 14, 2020, 03:51:26 am »
I know it is an old thread but examples here are not working, at least not in my case.

If 'IndexDefs.Add' is used then when posting a new record this error is shown: Project ... raised exception class 'External: SIGSEGV'.

Is using indexes with TBufDataset changed in last 6 years?

I am using Lazarus 2.0.10 and FPC 3.2.0 on Windows 10.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: TBufDataSet index
« Reply #5 on: February 04, 2023, 03:24:53 am »
I also visiting an old thread. Anything wrong with following codes? This does not work --- raises SIGSEGV error.


Code: Pascal  [Select][+][-]
  1.    // bds1: TBufDataSet
  2.  
  3.    bds1.LoadFromStream(AStream);
  4.    with bds1.IndexDefs.AddIndexDef do begin
  5.        name := 'pk0';
  6.        fields := 'pid;code';
  7.        Options := [ixUnique];
  8.    end;
  9.    bds1.IndexName := 'pk0';
  10.    bds1.Open;
  11.  

It doesn't help to define Indexes at design time --- before "LoadFromStream".

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: TBufDataSet index
« Reply #6 on: February 04, 2023, 08:10:59 am »
This is working for me:
Code: Pascal  [Select][+][-]
  1. DM.bdPostavke.Close;
  2. DM.bdPostavke.Clear;
  3. DM.bdPostavke.FieldDefs.Add('korisnik',ftString,20);
  4. DM.bdPostavke.FieldDefs.Add('racunalo',ftString,20);
  5. DM.bdPostavke.FieldDefs.Add('podrucje',ftString,20);
  6. DM.bdPostavke.FieldDefs.Add('postavka',ftString,128);
  7. DM.bdPostavke.FieldDefs.Add('rbr',ftInteger);
  8. DM.bdPostavke.FieldDefs.Add('ime',ftString,128);
  9. DM.bdPostavke.FieldDefs.Add('vrijednost',ftInteger);
  10.  
  11. ...
  12.  
  13. DM.bdPostavke.IndexDefs.Add('gk_idx','korisnik;racunalo;podrucje;postavka;rbr',[ixCaseInsensitive,ixUnique]);
  14. DM.bdPostavke.IndexName:='gk_idx';
  15.  
  16. DM.bdPostavke.CreateDataset;
  17. DM.bdPostavke.Open;

 

TinyPortal © 2005-2018