TurboDB VCL Component Library

Creating a Table at Run-time

See also

Previous  Top  Next

The CreateTable method of the TTdbTable components creates a new database table at runtime. The table structure is determined by the FieldDefsTdb property of the table. If you want to create a completely new table, you must clear FieldDefsTdb first and then add the TdbFieldDefs you want to assign to the new table.

TurboDB also supports the standard mechanism based on the FieldDefs property of TDataSet. While this is a great way to assert compatibility, FieldDefsTdb offers a greater control and makes available special features and field types not supported in the standard.

To clear the FieldDefsTdb,

Call FieldDefsTdb.Clear.

To add a TdbFieldDef to the FieldDefsTdb of the table,

1.Call FieldDefsTdb.Add,
2.Set the properties of the TdbFieldDef returned by this function.

To create a database table at runtime,

1.Set the FieldDefsTdb property according to the fields the new table should have,
2.Set the TableLevel property,
3.Set the Password property, if you want your table to be protected,
4.Set the DatabaseName and the TableName property
5.Call the CreateTable method.

Note: If there is already a table with this name, an exception will be raised.

The following code creates a new table with three columns:

TdbTable2.Close;
TdbTable2.FieldDefsTdb.Clear;
TdbTable2.FieldDefsTdb.Add('Word', dtString);
TdbTable2.FieldDefsTdb.Add('Count', dtSmallInt);
with TdbTable2.FieldDefsTdb.Add('RecordId', dtAutoInc) do Specification := 'Word';
TdbTable2.TableName := 'index';
TdbTable2.TableLevel := 6;
TdbTable2.CreateTable;
TdbTable2.Open;

 

To specify calculated table columns

1.When defining the columns of the table through the TTdbFieldDef objects, assign an expression to the CalcExpression property.
2.If the expression is to be used for calculating a new value for the column each time the row data changes, set the InternalCalcField property to True. If the expression shall be used to calculate a default value for the column, set InternalCalcField to False.