Product Documentation

c-treeDB API for C++ - Developers Guide

Previous Topic

Next Topic

Updating existing records

To update an existing record, you need to perform the following actions:

  1. Read the record from disk by calling one of the CTRecord::First(), CTRecord::Last(), CTRecord::Next(), CTRecord::Prev(), CTRecord::SeekRecord(), CTRecord::Find() or CTRecord::FindTarget() methods.
  2. Update one or more fields with one of the CTRecord::SetFieldAs...() methods.
  3. Write the record by calling CTRecord::Write().

// update the first record

ARecord.First();


// change the first field

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

// write the record

try

{

ARecord.Write();

}

catch (CTException &err)

{

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

}

A record is updated when the new record flag is cleared and the edited record flag is set. The edited record flag is set when a record is read from disk and it indicates that this is an existing record.

If you read a record from disk, and without updating the record data, call CTRecord::Write(), no data record is written to disk, since the edited record flag is cleared.

A record update can be forced by setting the record edited flag with a call to CTRecord::SetEdited() method.

// force update of first record

ARecord.First();


// set the edited flag

ARecord.SetEdited(YES);


// write the record

try

{

ARecord.Write();

}

catch (CTException &err)

{

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

}

A record can be duplicated by reading it from disk, setting the edited record flag by calling CTRecord::SetEdited() and setting the new record flag by calling CTRecord::SetNew(), and calling CTRecord::Write() to write it to disk. Please note that a record can only be duplicated if the table has no indexes with unique keys.

// duplicate the first record

ARecord.First();


// set the edited flag

ARecord.SetEdited(YES);


// set the new record flag

ARecord.SetNew(YES);


// write the record

try

{

ARecord.Write();

}

catch (CTException &err)

{

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

}

TOCIndex