Product Documentation

c-treeDB API API for C

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 ctdbFirstRecord(), ctdbLastRecord(), ctdbNextRecord(), ctdbPrevRecord(), ctdbSeekRecord(), ctdbFindRecord(), or ctdbFindTarget() functions.
  2. Update one or more fields with ctdbSetFieldAs() functions
  3. Write the record by calling ctdbWriteRecord().

/* update the first record */

ctdbFirstRecord(hRecord);


/* change the first field */

ctdbSetFieldAsString(hRecord, 0, "Joe Smith");


/* write the record */

if (ctdbWriteRecord(hRecord) != CTDBRET_OK)

printf("Update record failed\n");

c-treeDB API utilizes two flags to track the edited state of a record: new record and edited. When a record is newly created, the new record flag is set and the edited flag is cleared. The edited flag is set whenever data is moved into the record with any ctdbSetField() operations. When c-treeDB API prepares to write the record, it examines these flags. If this record has been edited, it checks the new record flag to either update an existing record, or add a new record. When you read an existing record with ctdbFirstRecord() or ctdbNextRecord(), etc, the new record flag is cleared and the edited flag is cleared. Again, the edited flag is only set if data is moved into the record with ctdbSetField() operations. If you request c-treeDB API to write the record and the edited flag is not set, no data is written. To force a record update to disk you should call ctdbSetEditedRecord() to set the edited flag directly.

/* update the first record */

ctdbFirstRecord(hRecord);


/* set the edited flag */

ctdbSetEditedRecord(hRecord, YES);


/* write the record */

if (ctdbWriteRecord(hRecord) != CTDBRET_OK)

printf("Update record failed\n");

Duplicate a record by: reading it from disk, setting the edited record flag with ctdbSetEditedRecord(), setting the new record flag with ctdbSetNewRecord(), and calling ctdbWriteRecord() 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 */

ctdbFirstRecord(hRecord);


/* set the edited flag */

ctdbSetEditedRecord(hRecord, YES);


/* set the new record flag */

ctdbSetNewRecord(hRecord, YES);


/* write the record */

if (ctdbWriteRecord(hRecord) != CTDBRET_OK)

printf("Duplicate record failed\n");

TOCIndex