Previous Topic

Next Topic

Adding, inserting or deleting fields

Fields are what hold the actual record data for each row in a table. Whereas a record could be considered a “row” in a table, a field could be considered a “column” in a table, or a “cell” in a record. The fields are defined at the time of the table creation.

Fields are added to the record definition in the order they are declared. The c-treeDB API also include a set of functions that will allow a field to be inserted at a certain place in the records definition and fields can also be deleted from the record definition.

ctdbAddField() adds a new field to the end of the record definition.

/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);
/* add two fields to the table record definition */
ctdbAddField(hTable, “Field1”, CT_INTEGER, 4);
ctdbAddField(hTable, “Field2”, CT_CHAR, 30);
/* create the table */
ctdbCreateTable(hTable, “MyTable”, CTCREATE_NORMAL);

ctdbInsField() and ctdbInsFieldByName() each insert a new field before a previously defined field. Use ctdbInsField() to specify the field index by its position in the record definition. The first field is number 0, the second field is number 1 and so on. Use ctdbInsFieldByName() to specify the field index by its name.

/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);
/* add two fields to the table record definition */
ctdbAddField(hTable, “Field1”, CT_INTEGER, 4);
ctdbAddField(hTable, “Field2”, CT_CHAR, 30);
ctdbInsFieldByName(hTable, “Field2”, “Field3”, CT_BOOL, 1);
/* create the table */
ctdbCreateTable(hTable, “MyTable”, CTCREATE_NORMAL);

ctdbDelField() and ctdbDelFieldByName() delete a field from the record definition. Use ctdbDelField() to delete a field specifying the field number. The first field is number 0, the second field is number 1, and so on. Use ctdbDelFieldByName() to delete a field specifying the field name.

/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);
/* add two fields to the table record definition */
ctdbAddField(hTable, “Field1”, CT_INTEGER, 4);
ctdbAddField(hTable, “Field2”, CT_CHAR, 30);
ctdbDelFieldByName(hTable, “Field2”);
/* create the table */
ctdbCreateTable(hTable, “MyTable”, CTCREATE_NORMAL);