Product Documentation

VCL/CLX Developers Guide

Previous Topic

Next Topic

Creating a Table Under Transaction Control

You can add an extra level of data integrity when creating a new table by placing the code to create a table inside a transaction. If the transaction is aborted, the table entry in the database dictionary file is removed, and the table data and index files are deleted from disk.

When a table is created inside a transaction, and until the transaction is committed or aborted, the newly created table must be opened with CTOPEN_EXCLUSIVE mode, otherwise the table open operation will fail. After the transaction is committed the table can be opened in non-exclusive mode.

The code fragment below creates a new table under transaction control. Again no error checking code is included in the example:

Delphi Example


procedure Form1.CreateATable();

begin

CtTable1.BeginTransaction;

try

CtTable1.Table := 'MyTable';

CtTable1.Database := CtDatabase1;

{$IFDEF WIN32} // Windows path

CtTable1.Directory := 'C:\Data';

{$ELSE} // Linux path

CtTable1.Directory := '/home/Data';

{$ENDIF}

CtTable1.AddField('Name', CT_STRING, 40);

CtTable1.AddField('Age', CT_INT2, 2);

CtTable1.AddField('Salary', CT_MONEY, 4);

CtTable1.AddIndex('Index01', CTINDEX_FIXED, True, False);

CtTable1.AddSegment('Index01', 'Name', CTSEG_USCHSEG);

CtTable1.CreateTable();

CtTable1.CommitTransaction;

except

on E : ECtError do

begin

CtTable1.AbortTransaction;

Application.ShowException(E);

end;

end;

end;

C++ Example


void Form1::CreateATable()

{

CtTable1->BeginTransaction();

try

{

CtTable1->Name = "MyTable";

CtTable1->Database = CtDatabase1;

#ifdef __WIN32__ // Windows path

CtTable1->Directory = "C:\\Data";

#else // Linux path

CtTable1->Directory = "/home/Data";

#endif

CtTable1->AddField("Name", CT_STRING, 40);

CtTable1->AddField("Age", CT_INT2, 2);

CtTable1->AddField("Salary", CT_MONEY, 4);

CtTable1->AddIndex("Index01", CTINDEX_ FIXED,

True, False);

CtTable1->AddSegment("Index01", "Name",

CTSEG_USCHSEG);

CtTable1->CreateTable();

CtTable1->CommitTransaction();

}

catch (ECtError& E)

{

CtTable1->AbortTransaction();

Application->ShowException(&E);

}

}

TOCIndex