Product Documentation

c-treeDB API for C

Previous Topic

Next Topic

Manage

Manage() provides data management functionality for your application and/or process.

Below is the code for Manage():

/*

* Manage()

*

* This function performs simple record functions of add, delete and gets

*/

#ifdef PROTOTYPE

VOID Manage(VOID)

#else

VOID Manage()

#endif

{

printf("MANAGE\n");

/* allocate a record handle */

if ((hRecord = ctdbAllocRecord(hTable)) == NULL)

Handle_Error("Manage(): ctdbAllocRecord()");

/* delete any existing records */

Delete_Records(hRecord);

/* populate the table with data */

Add_Records();

/* display contents of table */

Display_Records();

}

/*

* Delete_Records()

*

* This function deletes all the records in the table

*/

#ifdef PROTOTYPE

VOID Delete_Records(CTHANDLE hRecord)

#else

VOID Delete_Records(hRecord)

CTHANDLE hRecord;

#endif

{

CTDBRET retval;

CTBOOL empty;

printf("\tDelete records...\n");

empty = NO;

retval = ctdbFirstRecord(hRecord);

if (retval != CTDBRET_OK)

{

if (retval == END_OF_FILE)

empty = YES;

else

Handle_Error("Delete_Records(): ctdbFirstRecord()");

}

while (empty == NO) /* while table is not empty */

{

/* delete record */

if (ctdbDeleteRecord(hRecord))

Handle_Error("Delete_Records(): ctdbDeleteRecord()");

/* read next record */

retval = ctdbNextRecord(hRecord);

if (retval != CTDBRET_OK)

{

if (retval == END_OF_FILE)

empty = YES;

else

Handle_Error("Delete_Records(): ctdbNextRecord()");

}

}

}

/*

* Add_Records()

*

* This function adds records to a table in the database from an

* array of strings

*/

typedef struct {

CTSTRING number, zipcode, state, rating, name, address, city;

} CUSTOMER_DATA;

CUSTOMER_DATA data[] = {

"1000", "92867", "CA", "1", "Bryan Williams", "2999 Regency", "Orange",

"1001", "61434", "CT", "1", "Michael Jordan", "13 Main", "Harford",

"1002", "73677", "GA", "1", "Joshua Brown", "4356 Cambridge", "Atlanta",

"1003", "10034", "MO", "1", "Keyon Dooling", "19771 Park Avenue", "Columbia"

};

#ifdef PROTOTYPE

VOID Add_Records(VOID)

#else

VOID Add_Records()

#endif

{

CTDBRET retval;

CTSIGNED i;

CTSIGNED nRecords = sizeof(data) / sizeof(CUSTOMER_DATA);

printf("\tAdd records...\n");

/* add data to table */

for (i = 0; i < nRecords; i++)

{

/* clear record buffer */

ctdbClearRecord(hRecord);

retval = 0;

/* populate record buffer with data */

retval |= ctdbSetFieldAsString(hRecord, 0, data[i].number);

retval |= ctdbSetFieldAsString(hRecord, 1, data[i].zipcode);

retval |= ctdbSetFieldAsString(hRecord, 2, data[i].state);

retval |= ctdbSetFieldAsString(hRecord, 3, data[i].rating);

retval |= ctdbSetFieldAsString(hRecord, 4, data[i].name);

retval |= ctdbSetFieldAsString(hRecord, 5, data[i].address);

retval |= ctdbSetFieldAsString(hRecord, 6, data[i].city);

if (retval)

Handle_Error("Add_Records(): ctdbSetFieldAsString()");

/* add record */

if (ctdbWriteRecord(hRecord))

Handle_Error("Add_Records(): ctdbWriteRecord()");

}

}

/*

* Display_Records()

*

* This function displays the contents of a table. ctdbFirstRecord() and

* ctdbNextRecord() fetch the record. Then each field is parsed and displayed

*/

#ifdef PROTOTYPE

VOID Display_Records(VOID)

#else

VOID Display_Records()

#endif

{

CTDBRET retval;

TEXT custnumb[4+1];

TEXT custname[47+1];

printf("\tDisplay records...");

/* read first record */

retval = ctdbFirstRecord(hRecord);

if (retval != CTDBRET_OK)

Handle_Error("Display_Records(): ctdbFirstRecord()");

while (retval != END_OF_FILE)

{

retval = 0;

retval |= ctdbGetFieldAsString(hRecord, 0, custnumb, sizeof(custnumb));

retval |= ctdbGetFieldAsString(hRecord, 4, custname, sizeof(custname));

if (retval)

Handle_Error("Display_Records(): ctdbGetFieldAsString()");

printf("\n\t\t%-8s%10s\n",custnumb, custname);

/* read next record */

retval = ctdbNextRecord(hRecord);

if (retval == END_OF_FILE)

break; /* reached end of file */

if (retval != CTDBRET_OK)

Handle_Error("Display_Records(): ctdbNextRecord()");

}

}

TOCIndex