Link to home
Start Free TrialLog in
Avatar of k6__
k6__

asked on

BDE Field Sorting

Can somebody tell me how to sort my Database(Paradox using
BDE 4.51) by one Field ?

eg i want to sort by Database By the Field "NAME".
Thanx
Avatar of Matvey
Matvey

One way is to sort it phisically. Such procedure is available in the Database Desktop.
Another way is to create an index on the field "Name". You can create an index using Database Desktop and also from inside your program.
Another way is to use SQL:

SELECT * FROM <table_name> GROUP BY name;
Avatar of k6__

ASKER

I use TTable... Does TTable Accepts SQL Commands ?

Create a temporary index on your field
TTable doesn't allow SQL command. You can use TQuery component. It depands on the size of your table, but in most cases it's better to create an index on your field. I you create an index, the updates, deletes and appends to your table slow down. But if you don't have a big table, than you won't notice it. The SQL command can be made "live", and the TQuery can be used for updates too, but the command also takes time.
I must say that if you create an index on your field and then make an SQL call to your table that has already the index, the command will work super fast. So this is to say that you can combine the two things.
Anyway, I see that your task is only sorting, so as I said in most cases it's better just to make an index. How many records are stored in it?
Avatar of k6__

ASKER

well i want a PERMANT SORT!... and please post some code =)
i don't have a clue about indexes =)

Open your table in DataBase Desktop and in the restructure dialog you can define indexes.
Then in your TTable component set the IndexName property to the index name you defined in Database Desktop.
Your table will appear sorted by the field you defined the index on.
With the following codeexample you might be able to create a table with a primary and a secondary index:

Procedure CreateTable(Verzeichnis: TFileName);
var
      Table1: TTable;
begin
   Table1 := TTable.Create(Application);
   with Table1 do begin
      if not FileExists(Verzeichnis + '\DEMO.DB') then begin
         DataBaseName := Verzeichnis;
         Name := 'Table1';
         TableName := 'DEMO.DB';
         TableType := ttParadox;
         with FieldDefs do begin
            Clear;
            Add('Field1', ftString, 24, false);
            Add('Name',   ftString, 24, false);
            Add('Field2', ftString, 24, false);
            Add('Field3', ftString, 24, false);
            Add('Field4', ftString, 24, false);
            Add('Field5', ftString, 24, false);
            Add('Field6', ftString, 24, false);
            Add('Field7', ftString, 24, false);
            Add('Field8', ftString, 24, false);
         end;
         with IndexDefs do begin
            Clear;
            Add('Primary','Field1;Name', [ixPrimary, ixUnique]);
            Add('Name','Name', []);
         end;
         CreateTable;
      end;
      Free;
   end;
end;

In your Code you select the sort via:

   with Table1 do begin
      Active := False;
      IndexName := 'Name';
      Active := True;
   end;

There isn't more about indizes

Regards, Achim

Yes, of course it's possible to create indexes in run-time, but mostly useless in my opinion. Just some cases require this...
I don't think, it's useless.
1.) I seldom deliver the databases with my app. I create them and their indexes at runtime
2.) Creating an index at runtime enables me to sort a table temporary or pemanent on every field I want, i. e. if you click on a grid header (if the grid component supports that).
I also use it sometimes, but as k6__ said - he wants a permenant sort. Writing lots of code for this is sure possible, but not very useful.
Avatar of k6__

ASKER

Ok but can you tell me how to Sort Via Database Desktop
Permant ?

as matvey said :

Open your table in DataBase Desktop and in the restructure dialog you can define indexes.

aksystem: very usuful code but as i said above i want to have
permant indexes .. not to create them at run time =-)...
sorry
 

ok .. i went to database desktop but i only found Secondary indexes!.. where are the primaries ? =)


Avatar of k6__

ASKER

btw .. will those indexes speed up things like eg :
Table1.Locate('Field', Edit1.text , []);

btw : Thanx my friends for all your helps =)

ASKER CERTIFIED SOLUTION
Avatar of aksystem
aksystem

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
k6, yes, it should speed up any sorting/searching.