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 record adds and updates using locking

//

VOID Manage(VOID)

{

printf("MANAGE\n");

// populate the table with data

Add_CustomerMaster_Records();

// display contents of table

Display_Records();

// update a record under locking control

Update_CustomerMaster_Record();

// display again after update and effects of lock

Display_Records();

}

//

// Add_CustomerMaster_Records()

//

// This function adds records to table CustomerMaster from an

// array of strings

//

VOID Add_CustomerMaster_Records(VOID)

{

typedef struct {

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

} DATA_RECORD;

DATA_RECORD 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"

};

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

Delete_Records(MyRecord);

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

try

{

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

{

MyRecord.Clear();

// populate record buffer with data

MyRecord.SetFieldAsString(0, data[i].number);

MyRecord.SetFieldAsString(1, data[i].zipcode);

MyRecord.SetFieldAsString(2, data[i].state);

MyRecord.SetFieldAsString(3, data[i].rating);

MyRecord.SetFieldAsString(4, data[i].name);

MyRecord.SetFieldAsString(5, data[i].address);

MyRecord.SetFieldAsString(6, data[i].city);

// add record

MyRecord.Write();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Delete_Records()

//

// This function deletes all the records in the table

//

VOID Delete_Records(CTRecord& record)

{

CTBOOL found;

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

try

{

// enable session-wide lock flag

MySession.Lock(CTLOCK_WRITE_BLOCK);

// read first record

found = record.First();

while (found) // while records are found

{

// delete record

record.Delete();

// read next record

found = record.Next();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Display_Records()

//

// This function displays the contents of a table. First() and Next()

// fetch the record. Then each field is parsed and displayed

//

VOID Display_Records(VOID)

{

CTBOOL found;

TEXT custnumb[4+1];

TEXT custname[47+1];

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

try

{

// read first record

found = MyRecord.First();

while (found)

{

strcpy(custnumb, MyRecord.GetFieldAsString(0).c_str());

strcpy(custname, MyRecord.GetFieldAsString(4).c_str());

// display data

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

// read next record

found = MyRecord.Next();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

}

//

// Update_CustomerMaster_Records()

//

// Update one record under locking control to demonstrate the effects

// of locking

//

VOID Update_CustomerMaster_Record(VOID)

{

printf("\tUpdate record...\n");

try

{

// enable session-wide lock flag

MySession.Lock(CTLOCK_WRITE_BLOCK);

MyRecord.Clear();

MyRecord.SetFieldAsString(0, "1003");

// find record by customer number

if (MyRecord.Find(CTFIND_EQ))

{

MyRecord.SetFieldAsString(4, "KEYON DOOLING");

// rewrite record

MyRecord.Write();

printf("\tPress <ENTER> key to unlock\n");

getchar();

}

}

catch(CTException E)

{

Handle_Exception(E);

}

MySession.Unlock();

}

TOCIndex