Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Add Indexes to Table Created

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
ghuna388
Asked:
ghuna388
  • 8
  • 8
  • 2
  • +1
1 Solution
 
JaymolCommented:
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
 
ellessarCommented:
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
 
JaymolCommented:
Don't use the IndexDefs property - it's crap and doesn't seem to work very reliably.

John.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
MotazCommented:
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
 
MotazCommented:
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
 
JaymolCommented:
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
 
MotazCommented:
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
 
MotazCommented:
Another thing Jaymol, how can you create secondary indeces?
0
 
JaymolCommented:
It's not my fault you didn't refresh your browser.

You don't know how to creat a secondary index?
0
 
MotazCommented:
Yes
0
 
JaymolCommented:
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
 
MotazCommented:
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
 
JaymolCommented:
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
 
MotazCommented:
Yes, it works, thanks alot.

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

Motaz
www.geocities.com/motaz1
0
 
ghuna388Author Commented:
Hi Motaz,
the code u use create primary only but what about secondary index i want to create PrimaryIndex and secondary,

Abdelghani  
0
 
JaymolCommented:
No problem.  If you ever need any more help on DB's (mainly Paradox ones), let me know (Jaymol@hotmail.com)

John.
0
 
MotazCommented:
As I've told you Abdelghani, try to use Jaymol's comment it works without any problem.

Motaz
0
 
JaymolCommented:
Have you tried it yet?
0
 
ghuna388Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 8
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now