Product Documentation

FairCom Direct SQL for C and 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

*/

void Manage(void)

{

printf("MANAGE\n");

/* delete any existing records */

Delete_Records();

/* 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();

}

/*

* Delete_Records()

*

* This function deletes all the records in the table

*/

void Delete_Records(void)

{

CTSQLRET rc;

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

if ((rc = ctsqlExecuteDirect(hCmd, CT_STRING_LITERAL("DELETE FROM custmast"))) != CTSQLRET_OK)

Handle_Error("ctsqlExecuteDirect(DELETE)");

if ((rc = ctsqlCommit(hConn)) != CTSQLRET_OK)

Handle_Error("ctsqlCommit()");

}

/*

* Add_CustomerMaster_Records()

*

* This function adds records to a table from an array of strings

*/

void Add_CustomerMaster_Records(void)

{

CTSQLRET rc;

INTEGER i;

CTSQLCHAR sCommand[512];

CTSQLCHAR *data[] = {

CT_STRING_LITERAL("('1000','92867','CA','1','Bryan Williams','2999 Regency','Orange')"),

CT_STRING_LITERAL("('1001','61434','CT','1','Michael Jordan','13 Main','Harford')"),

CT_STRING_LITERAL("('1002','73677','GA','1','Joshua Brown','4356 Cambridge','Atlanta')"),

CT_STRING_LITERAL("('1003','10034','MO','1','Keyon Dooling','19771 Park Avenue','Columbia')")

};

INTEGER nRecords = sizeof(data) / sizeof(data[0]);

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

/* add one record at time to table */

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

{

ctsql_snprintf (sCommand, sizeof(sCommand)/sizeof(*sCommand), CT_STRING_LITERAL("INSERT INTO custmast VALUES %") CT_PRINTF_STR, data[i]);

if ((rc = ctsqlExecuteDirect(hCmd, sCommand)) != CTSQLRET_OK)

Handle_Error("ctsqlExecuteDirect(INSERT)");

}

if ((rc = ctsqlCommit(hConn)) != CTSQLRET_OK)

Handle_Error("ctsqlCommit()");

}

/*

* Display_Records()

*

* This function displays the contents of a table.

*/

void Display_Records(void)

{

CTSQLRET rc;

char custnumb[4+1];

char custname[47+1];

pCTSQLCURSOR hCursor = NULL;

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

rc = ctsqlPrepare(hCmd, CT_STRING_LITERAL("SELECT * FROM custmast"));

if (rc != CTSQLRET_OK)

Handle_Error("ctsqlPrepare(SELECT)");

rc = ctsqlExecute(hCmd, &hCursor);

if (rc != CTSQLRET_OK)

Handle_Error("ctsqlExecute(SELECT)");

/* fetch and display each individual record */

while ((rc = ctsqlNext(hCursor)) == CTSQLRET_OK)

{

ctsqlGetChar(hCursor, 0, custnumb);

ctsqlGetChar(hCursor, 4, custname);

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

}

ctsqlFreeCursor(hCursor);

}

/*

* Update_CustomerMaster_Records()

*

* Update one record under locking control to demonstrate the effects

* of locking

*/

void Update_CustomerMaster_Record(void)

{

CTSQLRET rc;

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

rc = ctsqlExecuteDirect(hCmd, CT_STRING_LITERAL("UPDATE custmast SET cm_custname = 'KEYON DOOLING' WHERE cm_custnumb = '1003'"));

if (rc != CTSQLRET_OK)

Handle_Error("ctsqlExecuteDirect(UPDATE)");

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

getchar();

if ((rc = ctsqlCommit(hConn)) != CTSQLRET_OK)

Handle_Error("ctsqlCommit()");

}

TOCIndex