Product Documentation

c-treeDB API for C++ - Developers Guide

Previous Topic

Next Topic

Adding new records

To add a new record to a table, you need to perform the following actions:

  1. Clear the record buffer
  2. Populate the fields by calling the methods CTRecord::SetFieldAs...()
  3. Add the record by calling CTRecord::Write()

// clear the record buffer

ARecord.Clear();


// populate the record buffer

ARecord.SetFieldAsSigned(0, 1000);

ARecord.SetFieldAsString(1, "Joe Smith");


// write the record

try

{

ARecord.Write();

}

catch (CTException &err)

{

printf("Add record failed with error %d\n", err.GetErrorCode());

}

A record is added to a table if the new record flag and edited flag are both set. CTRecord::Clear() sets the new record flag, while one of the CTRecord::SetFieldAs...() functions will set the edited record flag.

If you clear a record and, without populating the record buffer with data, a call to CTRecord::Write() returns no error code but no data record is written to disk, since the new record flag is set but the edited record flag is cleared.

If you wish to write a cleared or blank record to disk, you must clear the record buffer, set the edited record flag by calling ctdbSetEditedRecord() and then write the record by calling CTRecord::Write().

// clear the record buffer

ARecord.Clear();


// set the edited record flag

ARecord.SetEdited(YES);


// write the record

try

{

ARecord.Write();

}

catch (CTException &err)

{

printf("Write record failed with error %d\n", err.GetErrorCode());

}

TOCIndex