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) {
RETCODE rc; printf("\tDelete records...\n");
if ((rc = SQLExecDirect(hStmt, "DELETE FROM custmast", SQL_NTS)) != SQL_SUCCESS) Handle_Error(SQL_HANDLE_STMT, hStmt, "SQLExecDirect(DELETE)"); } /* * Add_Records() * * This function adds records to a table from an array of strings */ void Add_Records(void) {
RETCODE rc; SQLINTEGER i; SQLTCHAR sCommand[512]; SQLTCHAR *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')"
}; SQLINTEGER nRecords = sizeof(data) / sizeof(data[0]); printf("\tAdd records...\n");
/* add one record at time to table */ for (i = 0; i < nRecords; i++) {
strcpy (sCommand, "INSERT INTO custmast VALUES "); strcat (sCommand, data[i]); if ((rc = SQLExecDirect(hStmt, sCommand ,SQL_NTS)) != SQL_SUCCESS) Handle_Error(SQL_HANDLE_STMT, hStmt, "SQLExecDirect(INSERT)"); } } /* * Display_Records() * * This function displays the contents of a table. */ void Display_Records(void) {
RETCODE rc; SQLTCHAR custnumb[4+1]; SQLTCHAR custname[47+1]; SDWORD cbData; printf("\tDisplay records...");
rc = SQLExecDirect(hStmt, "SELECT * FROM custmast" ,SQL_NTS); if (rc != SQL_SUCCESS) Handle_Error(SQL_HANDLE_STMT, hStmt, "SQLExecDirect(SELECT)"); /* fetch and display each individual record */ while ((rc = SQLFetch(hStmt)) == SQL_SUCCESS) {
SQLGetData(hStmt, 1, SQL_C_CHAR, custnumb, sizeof(custnumb), &cbData); SQLGetData(hStmt, 5, SQL_C_CHAR, custname, sizeof(custname), &cbData); printf("\n\t\t%-8s%10s\n",custnumb, custname);
} SQLFreeStmt(hStmt,SQL_CLOSE); } |
|||