Creating a Table at Runtime
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,
1.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 and the Key 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, it will be overwritten. Be sure not to delete any important information, before you create the new table.

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 := 3;
TdbTable2.CreateTable;
TdbTable2.Open;