A valid table object is required before most table operations can take place. You need to pass a valid CTDatabase object to the CTTable constructor.
// create a CTTable object
CTTable ATable(ADatabase);
try
{
// open table "MyTable"
ATable.Open("MyTable", CTOPEN_NORMAL);
}
catch (CTException &err)
{
printf("Table open failed with error %d\n", err.GetErrorCode);
}
If you create a dynamic CTTable object with the new operator, you are required to destroy the object with the delete operator.
// create a dynamic CTTable object
CTTable* pTable = new CTTable(ADatabase);
if (!pTable)
{
printf("CTTable creation failed\n");
}
... other operations ..
// destroy the CTTable object
delete pTable;
If you destroy an active table object, all record objects associated with the table will be reset and the table closed before the object is destroyed.
It is possible to create or open a table without database support by passing a session object when creating the table object. Start your session with the CTSESSION_CTREE mode.
// create the session and table objects
CTSession ASession(CTSESSION_CTREE);
CTTable ATable(ASession);
// logon to session
ASession.Logon("FAIRCOMS", "ADMIN", "ADMIN");
// without database support, it is necessary to
// specify the path were the table is located
ATable.SetPath("C:\\MyDocuments");
// open the table
try
{
ATable.Open("MyTable", CTOPEN_NORMAL);
}
catch (CTException &err)
{
printf("Table open failed with error %d\n, err.GetErrorCode());
}
Please note from the code above the need to specify the path where the table is located. If no path is specified, c-treeDB will try to open the table from the current directory. The same principle applies when creating a table without database support:
// create the session and table objects
CTSession ASession(CTSESSION_CTREE);
CTTable ATable(ASession);
// logon to session
ASession.Logon("FAIRCOMS", "ADMIN", "ADMIN");
// add fields to table
ATable.AddField("Field1", CT_INT2, 2);
ATable.AddField("Field2", CT_FSTRING, 30);
// without database support, it is necessary to
// specify the path were the table is located
ATable.SetPath("C:\\MyDocuments");
// open the table
try
{
ATable.Create("MyTable", CTCREATE_NORMAL);
}
catch (CTException &err)
{
printf("Create open failed with error %d\n, err.GetErrorCode());
}