Solved

Add Indexes to Table Created

Posted on 2000-02-21
19
151 Views
Last Modified: 2010-04-04
I asked this question in another login
but the login is not available for me now.
and now this is the question:
   i had create a table at runtime using function below
function CreateParadoxTable(ATableName: string; const Args: array of const): TTable;
var
  i: integer;
  Table:TTable;
begin
  Table := TTable.Create(nil);
  with Table do
  begin
    // field active not true
    Active := false;
    DataBaseName := '';
    TableName := ATableName;
    TableType :=  ttdefault;  // paradox table
    Tablelevel := 7;   // Paradox 7
     for i :=  low(Args) to High(Args) div 4 do
          with FieldDefs do
            if Args[i * 4].VType <> vtAnsiString then
            raise  EInvalidArgument.CreateFmt('Invalid string format ',[Args[i * 4].VType] )
            else
            if Args[i * 4 + 1].VType <> vtInteger then
            raise  EInvalidArgument.CreateFmt('Invalid numeric format ',[Args[i * 4+1].VType] )
            else
            if Args[i * 4 + 2].VType <> vtInteger then
            raise  EInvalidArgument.CreateFmt('Invalid numeric format ',[Args[i * 4+2].VType] )
            else
            if Args[i * 4 + 3].VType <> vtBoolean then
            raise  EInvalidArgument.CreateFmt('Invalid boolean format ',[Args[i * 4+3].VType] )
            else
              Add(String(Args[i * 4].vAnsiString),    //Name of field
              tFieldType(Args[i * 4 + 1].vInteger),   //type of field
              Args[i * 4 + 2].VInteger,               // size of  field
              Args[i * 4 + 3].VBoolean);              // Requerid
     CreateTable;  // Create phsical table
  end;  // with table
  Result := Table;
end;


there is no Error here the table was created successful.
after that i want to add Indexes to that table .
i tryed with function below but it dosn't work:
function AddParadoxIndex(ATable: TTable;AIndexName: string; AFieldName: string; AOptions: TIndexOptions): boolean;
begin
   {there are sum validations here }
  Result := false;
  With (ATable.IndexDefs.AddIndexDef) do
  begin
    Name := AIndexName;
    Fields := AFieldName;
    Options := AOptions;
  end;
  Result := True;
end;


and that is all.
i wish you understand what i mean.
   
   Abdelghani
 Thanks and Regard me
0
Comment
Question by:ghuna388
  • 8
  • 8
  • 2
  • +1
19 Comments
 
LVL 6

Expert Comment

by:Jaymol
ID: 2541936
Just use AddIndex.  Make sure the table is not active and use the above command - Really easy to use.

tblMain.AddIndex('', 'OrderNumber', [ixPrimary]);

(Crappy example, but that's it!)

NEVER give a primary index a name.

Hope this helps.

John.
0
 

Expert Comment

by:ellessar
ID: 2541937
Can you create the indexes when you create the table, or must the indexes be created later on??

Here is a sample of code that I have used to create tables with indexes:


with tblGenerate do
begin
 // create the correct Table Type
  TableType := ttParadox;

  { Next, describe the fields in the table }
  with FieldDefs do
  begin
    Clear;
    Add('ID', ftInteger,   0, True);
    Add('Surname', ftString,  120, True);  
    Add('FirstNames', ftString, 90, False);
    Add('Initials', ftString, 5 , True);
   end;

   { Next, describe any indexes }
   with IndexDefs do
   begin
      Clear;
    { The first index has no name because it is a Paradox primary key }
      Add('', 'ID', [ixPrimary, ixUnique]);
      Add('Name_Ndx',  'Surname', []);
   end;

   CreateTable;
end;
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2541942
Don't use the IndexDefs property - it's crap and doesn't seem to work very reliably.

John.
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2541997
Abdel Ghani, try this:

procedure CreateIndex(ATableName, AFieldsName: string;
  AOptions: TIndexOptions);
var
  Table: TTable;
begin
  Table:= TTable.Create(nil);
  Table.TableName:= ATableName;
  Table.AddIndex(AFieldsName + 'Index', AFieldsName, AOptions);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateIndex('D:\Test\Test.db', 'ID', [ixPrimary]);
end;

But this works only for primary index, and I'll try to make it work with other secondary indices.

Motaz
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2542013
These can work with a Table like:

ID +
Name A  30
Age  N

Make sure that the primary index is in the first, for example it should not work with:

Name A 30
ID  +
Age N

- You can also define more than one field as a primary indeces:

CreateIndex('D:\Test\Test.db', 'ID;Name', [ixPrimary]);

Motaz
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2542059
Erm hello....that's what I suggested.  Why do people insist on wrapping a function or procedure declaration around other people's code and then suggesting it's there own?

You don't need the procedure, just the one line of code I gave you.

John.
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2542076
Jaymol, I write my comment here befor I saw your comment. And this comment is mine I do not copy it from you and make it in a procedure.
Did you understand !!
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2542082
Another thing Jaymol, how can you create secondary indeces?
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2542103
It's not my fault you didn't refresh your browser.

You don't know how to creat a secondary index?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Expert Comment

by:Motaz
ID: 2542111
Yes
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2542125
It's exactly the same as using the AddIndex method to create a primary index.

tblMain.AddIndex('Secondary', 'CompanyName', [ixUnique]);

Just alter the table's IndexName to suit whichever index you wish to use.

John.
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2542152
Yes, it works.
I forget to pass ixUnique, I send it as a blank options. Cann't I use secondary index that can be dublicated?
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2542170
Secondary indexes do not have to be unique.  If you take out the ixUnique and leave the brackets empty it should work.  If not, make sure to include another field that is unique.

eg. Your table has an 'ID' which is unique and a 'Name' which is not.  Do this to create Primary and Secondary indexes....

    With tblMain do begin
      AddIndex('', 'ID', [ixPrimary]);
      AddIndex('Secondary', 'Name;ID', [ixUnique]);
    end;

That should do the trick.  (That is from memory, so it may be slightly wrong.  If it is, I'll look it up in some of my code for you.)
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2542181
Yes, it works, thanks alot.

Abdelghani, try to see Jaymol comments, it could help you.

Motaz
www.geocities.com/motaz1
0
 

Author Comment

by:ghuna388
ID: 2542191
Hi Motaz,
the code u use create primary only but what about secondary index i want to create PrimaryIndex and secondary,

Abdelghani  
0
 
LVL 6

Accepted Solution

by:
Jaymol earned 100 total points
ID: 2542192
No problem.  If you ever need any more help on DB's (mainly Paradox ones), let me know (Jaymol@hotmail.com)

John.
0
 
LVL 7

Expert Comment

by:Motaz
ID: 2542228
As I've told you Abdelghani, try to use Jaymol's comment it works without any problem.

Motaz
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 2542398
Have you tried it yet?
0
 

Author Comment

by:ghuna388
ID: 2542408
Hi Jaymol,
I'm sorry  i'm not in my ooffice for long time when i arrive  i test Motaz's comment  because  i'm not refresh my Browser .........
Abdelghani
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now