Previous Topic

Next Topic

Manage

The manage step 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
//

VOID Manage(VOID)
{
   printf("MANAGE\n");

   // delete any existing records
   Delete_Records();

   // populate the table with data
   Add_Records();

   // display contents of table
   Display_Records();
}

//
// Delete_Records()
//
// This function deletes all the records in the table
//
VOID Delete_Records(VOID)
{
   CTBOOL   found;

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

   try
   {
      // read first record
      found = MyRecord.First();

      while (found)  // while records are found
      {
         // delete record
         MyRecord.Delete();
         // read next record
         found = MyRecord.Next();
      }
   }
   catch(CTException E)
   {
      Handle_Exception(E);
   }
}

//
// Add_Records()
//
// This function adds records to a table in the database from an
// array of strings
//

VOID Add_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);

   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);
   }
}

//
// 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[47+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());

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

         // read next record
         found = MyRecord.Next();
      }
   }
   catch(CTException E)
   {
      Handle_Exception(E);
   }
}