Product Documentation

c-treeDB API API for C

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 by calling ctdbClearRecord().
  2. Populate the fields by calling the ctdbSetFieldAs() functions.
  3. Add the record by calling ctdbWriteRecord():

/* add new record */

ctdbClearRecord(hRecord);


/* populate the record buffer */

ctdbSetFieldAsSigned(hRecord, 0, 1000);

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


/* write the record */

if (ctdbWriteRecord(hRecord) != CTDBRET_OK)

printf("Add record failed\n");

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

If you clear a record without populating the record buffer with data, ctdbWriteRecord() returns no error code. However, 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 ctdbWriteRecord().

/* add a blank record to disk */

ctdbClearRecord(hRecord);


/* set the edited record flag */

ctdbSetEditedRecord(hRecord, YES);


/* write the record */

if (ctdbWriteRecord(hRecord) != CTDBRET_OK)

printf("Add record failed\n");

TOCIndex