#1 2013-01-03 10:49:12

delphiboy
Member
Registered: 2013-01-03
Posts: 1

Problem using Search method

Hi. I'm using the following code to create a database and add a few records to it:

procedure TForm1.Button1Click(Sender: TObject);
var
  Table: TSynBigTableRecord;
  rec1, rec2: TSynTableData;
begin
  Table := TSynBigTableRecord.Create('FileName.ext','Table1');
  Table.AddField('name',tftUTF8,[tfoIndex]);
  Table.AddField('id',tftInt32,[tfoIndex,tfoUnique]);
  Table.AddFieldUpdate;
  rec1.Init(Table.Table);
  rec1.Field['id']:=1;
  rec1.Field['name']:='John';
  Table.RecordAdd(rec1);
  rec2.Init(Table.Table);
  rec2.Field['id']:=2;
  rec2.Field['name']:='Jack';
  Table.RecordAdd(rec2);
  Table.UpdateToFile();
  Table.Free;
end;

The two records are added successfully and stored in the file (I checked file contents). But when I try using the Search method, it doesn't return anything. For example, I want to find all records with 'id' field set to 1 (which should return one record) :

procedure TForm1.Button2Click(Sender: TObject);
var
  Table: TSynBigTableRecord;
  IDs: TIntegerDynArray;
  IDCount:integer;
  fint: TSynTableFieldProperties;
begin
Table := TSynBigTableRecord.Create('FileName.ext','Table1');
fint:= Table.Table['id'];
Table.Search(fint, Fint.SBF(1), IDs, IDCount);
Table.Free;
end;

I get an 'Access Violation' error with this code. I also tried searching in 'name' field. The following code didn't get that exception, but it does not find any records (IDCount is 0):

procedure TForm1.Button4Click(Sender: TObject);
var
  Table: TSynBigTableRecord;
  IDs: TIntegerDynArray;
  IDCount:integer;
  FieldText: TSynTableFieldProperties;
begin
Table := TSynBigTableRecord.Create('FileName.ext','Table1');
FieldText := Table.Table['name'];
Table.Search(FieldText, FieldText.SBF('John'), IDs, idcount);
Table.Free;
end;

Can you please help me to solve both problems explained above (1.Exception when searching 'id'         2.No results when searching 'name')?
Tell what I'm doing wrong. Thanks in advance.

Offline

#2 2013-03-14 07:10:04

sh17
Member
Registered: 2011-08-09
Posts: 25

Re: Problem using Search method

try this: (the word 'id' as fieldname is reserved)

var
  Table: TSynBigTableRecord;
  rec1, rec2: TSynTableData;
  IDs: TIntegerDynArray;
  IDCount:integer;
  fint,FieldText: TSynTableFieldProperties;
begin
  DeleteFile('D:\temp\ff.idx');
  Table := TSynBigTableRecord.Create('D:\temp\ff.idx','Table1');
  Table.AddField('name',tftUTF8,[tfoIndex]);
  Table.AddField('mid',tftInt32,[tfoIndex,tfoUnique]);
  Table.AddFieldUpdate;
  rec1.Init(Table.Table);
  rec1.Field['mid']:=1;
  rec1.Field['name']:='John';
  Table.RecordAdd(rec1);
  rec2.Init(Table.Table);
  rec2.Field['mid']:=2;
  rec2.Field['name']:='Jack';
  Table.RecordAdd(rec2);
  Table.UpdateToFile();
  Table.Free;


  Table := TSynBigTableRecord.Create('D:\temp\ff.idx','Table1');
  fint:= Table.Table['mid'];
  Table.Search(fint, Fint.SBF(1), IDs, IDCount);
  Table.Free;

  Table := TSynBigTableRecord.Create('D:\temp\ff.idx','Table1');
  FieldText := Table.Table['name'];
  Table.Search(FieldText, FieldText.SBF('John'), IDs, idcount);
  Table.Free;

Offline

Board footer

Powered by FluxBB