Some table type datasets let you create and delete the underlying tables at design time or at runtime. Typically, database tables are created and deleted by a database administrator. However, it can be handy during application development and testing to create and destroy database tables that your application can use.
TTable and TIBTable both let you create the underlying database table without using SQL. Similarly, TClientDataSet lets you create a dataset when you are not working with a dataset provider. Using TTable and TClientDataSet, you can create the table at design time or runtime. TIBTable only lets you create tables at runtime.
Before you can create the table, you must be set properties to specify the structure of the table you are creating. In particular, you must specify
To create the table at runtime, call the CreateTable method (TTable and TIBTable) or the CreateDataSet method (TClientDataSet).
var TableFound: Boolean; begin with TTable.Create(nil) do // create a temporary TTable component begin try { set properties of the temporary TTable component } Active := False; DatabaseName := 'DBDEMOS'; TableName := Edit1.Text; TableType := ttDefault; { define fields for the new table } FieldDefs.Clear; with FieldDefs.AddFieldDef do begin Name := 'First'; DataType := ftString; Size := 20; Required := False; end; with FieldDefs.AddFieldDef do begin Name := 'Second'; DataType := ftString; Size := 30; Required := False; end; { define indexes for the new table } IndexDefs.Clear; with IndexDefs.AddIndexDef do begin Name := ''; Fields := 'First'; Options := [ixPrimary]; end; TableFound := Exists; // check whether the table already exists if TableFound then if MessageDlg('Overwrite existing table ' + Edit1.Text + '?', mtConfirmation, mbYesNoCancel, 0) = mrYes then TableFound := False; if not TableFound then CreateTable; // create the table finally Free; // destroy the temporary TTable when done end; end; end;
TTable *NewTable = new TTable(Form1); NewTable->Active = false; NewTable->DatabaseName = "BCDEMOS"; NewTable->TableName = Edit1->Text; NewTable->TableType = ttDefault; NewTable->FieldDefs->Clear(); TFieldDef *NewField = NewTable->FieldDefs->AddFieldDef(); // define first field NewField->DataType = ftInteger; NewField->Name = Edit2->Text; NewField = NewTable->FieldDefs->AddFieldDef(); // define second field NewField->DataType = ftString; NewField->Size = StrToInt(Edit3->Text); NewField->Name = Edit4->Text; NewTable->IndexDefs->Clear(); TIndexDef *NewIndex = NewTable->IndexDefs->AddIndexDef(); // add an index NewIndex->Name = "PrimaryIndex"; NewIndex->Fields = Edit2->Text; NewIndex->Options << ixPrimary << ixUnique; // Now check for prior existence of this table bool CreateIt = (!NewTable->Exists); if (!CreateIt) if (Application->MessageBox((AnsiString("Overwrite table ") + Edit1->Text + AnsiString("?")).c_str(), "Table Exists", MB_YESNO) == IDYES) CreateIt = true; if (CreateIt) NewTable->CreateTable(); // create the table
TTable and TIBTable let you delete tables from the underlying database table without using SQL. To delete a table at runtime, call the dataset's DeleteTable method. For example, the following statement removes the table underlying a dataset:
CustomersTable.DeleteTable;
CustomersTable->DeleteTable();
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|