Solved

Add Indexes to Table Created

Posted on 2000-02-21
19
155 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

15 Experts available now in Live!

Get 1:1 Help Now