To add a new record to a table, you need to perform the following actions:
// 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());
}