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 get

*/

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)

{

CTSQLRET rc;

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

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

Handle_Error("ctsqlExecuteDirect(DELETE)");

}

/*

* Add_Records()

*

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

*/

void Add_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)");

}

}

/*

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

}

/*

* Handle_Error()

*

* General error routine that retrieves and displays specific SQL Error

* before exiting the tutorial. If the error returned indicates an object

* already exists, execution is returned to the calling function.

*/

void Handle_Error(char *sErrMsg)

{

CTSQLRET rc;

const INTEGER TABLE_ALREADY_EXISTS = -20041;

const INTEGER INDEX_ALREADY_EXISTS = -20028;

if (!hConn) {

printf("%s - SQL ERROR: %s\n", sErrMsg, "Out of memory");

} else {

rc = ctsqlGetError (hConn);

if (rc == TABLE_ALREADY_EXISTS || rc == INDEX_ALREADY_EXISTS)

return;

printf("%s - SQL ERROR: [%d] - %" CT_PRINTF_STR "\n", sErrMsg, rc, ctsqlGetErrorMessage(hConn));

if (rc == -20212)

printf("Perhaps your c-tree server is not running?\n");

}

printf("*** Execution aborted *** \nPress <ENTER> key to exit...");

getchar();

exit(1);

}

TOCIndex