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_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, "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[] = {

"('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')"

};

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_strcpy (sCommand, "INSERT INTO custmast VALUES ");

ctsql_strcat (sCommand, 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;

CTSQLCHAR custnumb[4+1];

CTSQLCHAR custname[47+1];

pCTSQLCURSOR hCursor = NULL;

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

rc = ctsqlPrepare(hCmd, "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)

{

ctsqlGetNChar(hCursor, 0, custnumb);

ctsqlGetNChar(hCursor, 4, custname);

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

}

ctsqlFreeCursor(hCursor);

}

TOCIndex