Solved

Add Indexes to Table Created

Posted on 2000-02-21
19
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 114
Run video youtube webbrowse 10 86
Create a path if not exists 7 125
Adoquery sql  left join does not work 25 120
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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