Databases

Check if table is empty

Posted in Databases

Use RecordCount property of Table component. If RecordCount is 0, then table is empty.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Table1.RecordCount=0 then
    Label1.Caption:='Table is emply'
  else
    Label1.Caption:='Table is not emply';
end;

Check if BDE installed

Posted in Databases

If you want to know is dbe installed, then you may use dbiInit function. And don't forget to add DbiProcs in uses chapter

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    dbiInit(nil);
  except
    ShowMessage('BDE is not installed');
  end;
  dbiExit;
end;

Add new index file at runtime

Posted in Databases

Use AddIndex method of Table component. First parameter is a name of new index, second parameter is a field of database.

procedure TForm1.Button1Click(Sender: TObject);
var
  Str: string;
begin
  Str:='Area';
  Table1.AddIndex(Str, Str, [ixUnique]);
end;

Add field to table at runtime

Posted in Databases

Use standard methods and properties (FieldName, DataSet and ect.) of table component.

procedure TForm1.Button1Click(Sender: TObject);
var
  Field: TField;
  i: Integer;
begin
  Table1.Active:=False;
  for i:=0 to Table1.FieldDefs.Count-1 do
    Field:=Table1.FieldDefs[i].CreateField(Table1);

  Field:=TStringField.Create(Table1);
  with Field do
  begin
    FieldName:='New Field';
    Calculated:=True;
    DataSet:=Table1;
  end;
  Table1.Active:=True;
end;

Accelerate first access to table

Posted in Databases

First access to any table is very slow. This problem may come from the fact that the BDE must query the DB to get table information before working with the table the first time. Once it has the information, it is able to cache it and will work quickly throughout the remainder of the session (while TDatabase.Connection remains True). To use the cached information between runs, go to the BDE config program to the particular alias you are using to connect through and set BDE CACHE = TRUE and BDE CACHE DIR = 'C:\temp' or wherever you want the files to be stored. Note: Be aware that if you change your table definition, you will need to delete the file in this Cache dir that represents it. You can find the filename in the SCache.INI file located in the same directory by viewing it in your favorite text editor.