🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.database.dbexpress
9 messages
9 total messages Started by MR Fri, 22 Jun 2007 09:03
How to create (dynamically) a primary index with more than one field?
#43
Author: MR
Date: Fri, 22 Jun 2007 09:03
78 lines
2850 bytes
How to create (dynamically) a primary index with *more than one
field*?

I tried the following:

    TTable *table; 
    table=new TTable(NULL);
    table->Active=false;
    table->TableType=ttDefault; // uses adequate table-type (e.g.
ttParadox, ttDBase) automatically, dependent on the file-extention
    table->TableType=ttParadox;
    table->TableName=EditBDETableName->Text;
    table->FieldDefs->Clear();
    table->FieldDefs->Add("Id", ftString, 10, FALSE);
    ...
    table->FieldDefs->Add("State", ftString, 25, FALSE);
    table->IndexDefs->Clear();

and than

    table->IndexDefs->Add("", "Id;Remark",
TIndexOptions()<<ixPrimary);

or instead

  table->IndexDefs->AddIndexDef()->Name="";
  table->IndexDefs->AddIndexDef()->Fields="Id"/*";Remark"*/;

table->IndexDefs->AddIndexDef()->Options=TIndexOptions()<<ixPrimary/*<<ixUnique*/;

and finally 

    table->CreateTable();
    table->Free();

When CreateTable() is executed I got an -exception
  'Zahl au�erhalb des g�ltigen Bereichs
   Tabelle nicht vorhanden 
   Tabelle: ...
translated
  'Number outside the valid range
   Table not available
   Table: ...

What did I do wrong?

Thanks,

Michael
I do not get this exception when using

    table->IndexDefs->Add("", "Id", TIndexOptions()<<ixPrimary);

, but unfortunatly 'Id' is not sufficent (i.e. there might be more
than one db-entry with the same id)




    /*  { Indizes der neuen Tabelle definieren }
        IndexDefs.Clear;
        with IndexDefs.AddIndexDef do begin
          Name := '';
          Fields := 'First';
          Options := [ixPrimary];
        end;
    */
    // table->IndexDefs->AddIndexDef()->Name="";
    // table->IndexDefs->AddIndexDef()->Fields="Id"/*";Remark"*/;
    //
table->IndexDefs->AddIndexDef()->Options=TIndexOptions()<<ixPrimary;

    // table->IndexDefs->Add("", "Id;Remark",
TIndexOptions()<<ixPrimary/*<<ixUnique*/);

    table->CreateTable();
    table->Free();
  }
Re: How to create (dynamically) a primary index with more than one field?
#44
Author: MR
Date: Sat, 23 Jun 2007 06:48
5 lines
976 bytes
Because I did not get an answer here and I found a quite
better-fitting group (BDE) I reposted it into this group.


Michael
Re: How to create (dynamically) a primary index with more than one field?
#45
Author: Minas
Date: Sun, 24 Jun 2007 08:22
58 lines
2674 bytes
Create a folder "C:\\TestDBTable" and
try a minimum piece of code.If the code is working
then you can add step by step additional fields in your table

    TTable *table;
    table=new TTable(NULL);
    table->Active=false;
    table->TableType=ttDefault;
    table->TableType=ttParadox;
    table->DatabaseName = "C:\\TestDBTable";
    table->TableName= EditBDETableName->Text;
    table->FieldDefs->Clear();
    table->FieldDefs->Add("Id", ftString, 10, FALSE);
    table->FieldDefs->Add("Remark", ftInteger, 0, FALSE);
    table->FieldDefs->Add("State", ftString, 25, FALSE);
    // .....  add additional fields here .......
    table->IndexDefs->Clear();
    table->IndexDefs->Add("", "Id;Remark",TIndexOptions()<<ixPrimary << 
ixUnique);
    //table->IndexDefs->Add("StateX", "State",TIndexOptions()<< 
ixCaseInsensitive);
    table->CreateTable();
    delete table;

You can use Database Explorer to verify the fields of
table Indices

Best Regards

_Minas Harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle

<MR> wrote

> How to create (dynamically) a primary index with *more than one
> field*?
>
> I tried the following:
>
>    TTable *table;
>    table=new TTable(NULL);
>    table->Active=false;
>    table->TableType=ttDefault; // uses adequate table-type (e.g.
> ttParadox, ttDBase) automatically, dependent on the file-extention
>    table->TableType=ttParadox;
>    table->TableName=EditBDETableName->Text;
>    table->FieldDefs->Clear();
>    table->FieldDefs->Add("Id", ftString, 10, FALSE);
>    ...
>    table->FieldDefs->Add("State", ftString, 25, FALSE);
>    table->IndexDefs->Clear();
>
> and than
>
>    table->IndexDefs->Add("", "Id;Remark",
> TIndexOptions()<<ixPrimary);
Re: How to create (dynamically) a primary index with more than one field?
#47
Author: MR
Date: Wed, 27 Jun 2007 06:56
20 lines
1545 bytes
Hello Minas,

thanks for your sample...

Trying around a lot and wondering why your sample does work and mine
did not, I finally found out, that the reason is, when indexing over
more than one field, the fields being index *must* be subsequent.

So inserting a FieldDef between Id and Remark in your sample will
result in the same exception I described.

Is this always the case or is this specific for the data-base type,
i.e. Paradox.

Or is there a way to allow non-subsequent fields being indexed too?


Thanks,

Michael
Re: How to create (dynamically) a primary index with more than one field?
#48
Author: MR
Date: Wed, 27 Jun 2007 07:27
14 lines
1298 bytes
<MR> schrieb:

>Hello Minas,
>
>thanks for your sample...
>
>Trying around a lot and wondering why your sample does work and mine
>did not, I finally found out, that the reason is, when indexing over
>more than one field, the fields being index *must* be subsequent.

It even seems, that it has to start with the very first field of the
record...

Michael
Re: How to create (dynamically) a primary index with more than one field?
#49
Author: Minas
Date: Wed, 27 Jun 2007 09:06
81 lines
3460 bytes
? <MR> wrote

> So inserting a FieldDef between Id and Remark in your sample will
> result in the same exception I described.
>
> Is this always the case or is this specific for the data-base type,
> i.e. Paradox.
>
> Or is there a way to allow non-subsequent fields being indexed too?
>

You are right.Key Fields for the primary index must be consecutive.I tried 
the same code
in a .mdb database and I get the same exception. To stop the exception I 
used
the other way ,
TFieldDef *fldDef = table->FieldDefs->AddFieldDef();

and I used FieldNo to specify the physical field order.
In this case , though , I got a mess in the fields of indices


    TTable *table;
    table=new TTable(NULL);
    table->Active=false;
    table->TableType=ttParadox;
    table->DatabaseName =  "C:\\TestDBTable";// Database1->DatabaseName;
    table->TableName= Edit1->Text;
    table->FieldDefs->Clear();
    //fields definition
    TFieldDef *fldDef = table->FieldDefs->AddFieldDef();
    fldDef->Name ="Id";
    fldDef->DataType = ftString;
    fldDef->Size =10;
    fldDef->Required =false;
    fldDef->FieldNo =1;

    fldDef = table->FieldDefs->AddFieldDef();
    fldDef->Name ="OtherFld";
    fldDef->DataType = ftString;
    fldDef->Size =25;
    fldDef->Required =false;
    fldDef->FieldNo =3;

    fldDef = table->FieldDefs->AddFieldDef();
    fldDef->Name ="State";
    fldDef->DataType = ftString;
    fldDef->Size =25;
    fldDef->Required =false;
    fldDef->FieldNo =4;

    fldDef = table->FieldDefs->AddFieldDef();
    fldDef->Name ="Remark";
    fldDef->DataType = ftInteger;
    fldDef->Size =0;
    fldDef->Required =false;
    fldDef->FieldNo =2;

    table->IndexDefs->Clear();
//    TIndexDef *idxDef = table->IndexDefs->AddIndexDef();
//    idxDef->Name="";
//    idxDef->Fields = "Id;Remark";
//    idxDef->Options << ixPrimary;
    // indices
    table->IndexDefs->Add("", "Id;Remark",TIndexOptions()<<ixPrimary);
    table->IndexDefs->Add("StateX", "State",TIndexOptions()<< 
ixCaseInsensitive);
    table->IndexDefs->Add("OtherX", "OtherFld",TIndexOptions()<< 
ixCaseInsensitive);
    table->CreateTable();
    delete table;

To get right results with no exception, Remark field must be defined next to
Id field definition .

Best Regards

_Minas Harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle 
Re: How to create (dynamically) a primary index with more than one field?
#50
Author: MR
Date: Wed, 27 Jun 2007 14:08
19 lines
1739 bytes
>You are right.Key Fields for the primary index must be consecutive.I tried 
>the same code in a .mdb database and I get the same exception.

Hmmm? The same in Access?

This is a little bit strange restriction, isn't it?
Is there a reason for this?

Unfortunately in BDS2006 I have not found a way to create a database
by something like a database-explorer to be able to create a
BDE-Paradox-database, like in the former versions? (Perhaps I forgot
to install something or it is a little bit more complicated (or they
really threw it out)? Is this restriction here too? 

But I suppose I could live with this need of reordering my fields...

Thanks,

Michael
Re: How to create (dynamically) a primary index with more than one field?
#51
Author: Minas
Date: Thu, 28 Jun 2007 01:43
43 lines
2311 bytes
<MR> wrote:


> Hmmm? The same in Access?
>

In Access I got this only when I set FieldDef->FieldNo property.Otherwise is 
working fine.

> by something like a database-explorer to be able to create a
> BDE-Paradox-database, like in the former versions? (Perhaps I forgot
> to install something or it is a little bit more complicated (or they
> really threw it out)? Is this restriction here too?

If you mean Database Desktop (I have it in BCB5 PRO), yes ,the fields must 
be consecutive .
I  have Turbo C++ Explorer which has only Database Explorer.

From the help: "Paradox:The fields in a key must be contiguous starting with 
the first field"
An easy way is to use SQL in Database Explorer(since you haven't DB Desktop) 
something like:

create table testTbl(
Id integer,
State varchar(25),
Remark varchar(10),
PRIMARY KEY (Id,Remark)
)

and for additional indices

create index StateX ON testTbl (State)

> But I suppose I could live with this need of reordering my fields...

Sure .Is not a big problem anyway.


_Minas Harokopos

------
"Only the virtue's conquests have certainty" Sofokleous Erephyle
Re: How to create (dynamically) a primary index with more than one field?
#52
Author: MR
Date: Sat, 30 Jun 2007 08:05
3 lines
1130 bytes
Thank you,

Michael
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads