ODBC Auto-Commit On

Go back to: Code Samples
/* For conciseness, this code omits error checking */
 
/* Allocate a statement handle */
rc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
 
/* Prepare an INSERT statement for multiple executions */
strcpy(sqlStatement, "INSERT INTO employees VALUES (?, ?, ?)");
rc = SQLPrepare((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Bind parameters */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
                      SQL_C_SLONG, SQL_INTEGER, 10, 0,
                      &id, sizeof(id), NULL);
rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT,
                      SQL_C_CHAR, SQL_CHAR, 20, 0,
                      name, sizeof(name), NULL);
rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT,
                      SQL_C_SLONG, SQL_INTEGER, 10, 0,
                      &salary, sizeof(salary), NULL);
 
/* Set parameter values before execution */
id = 20;
strcpy(name, "Employee20");
salary = 100000;
rc = SQLExecute(hstmt);
 
/* A commit occurs because auto-commit is on */
 
/* Change parameter values for the next execution */
id = 21;
strcpy(name, "Employee21");
salary = 150000;
rc = SQLExecute(hstmt);
 
/* A commit occurs because auto-commit is on */
 
/* Reset parameter bindings */
rc = SQLFreeStmt((SQLHSTMT)hstmt, SQL_RESET_PARAMS);
strcpy(sqlStatement, "SELECT id, name, salary FROM employees");
 
/* Execute a SELECT statement. A prepare is unnecessary
   because it’s executed only once. */
rc = SQLExecDirect((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Fetch the first row */
rc = SQLFetch(hstmt);
while (rc != SQL_NO_DATA_FOUND) {
 
/* All rows are returned when fetch returns SQL_NO_DATA_FOUND */
 
   /* Get the data for each column in the result set row */
   rc = SQLGetData (hstmt, 1, SQL_C_LONG, &id,
                    sizeof(id), NULL);
   rc = SQLGetData (hstmt, 2, SQL_C_CHAR, &name,
                    sizeof(name), NULL);
   rc = SQLGetData (hstmt, 3, SQL_C_LONG, &salary,
                    sizeof(salary), NULL);
   printf("\nID: %d Name: %s Salary: %d", id, name, salary);
 
   /* Fetch the next row of data */
   rc = SQLFetch(hstmt);
}
 
/* Close the cursor */
rc = SQLFreeStmt((SQLHSTMT)hstmt, SQL_CLOSE);
 
/* Whether a commit occurs after a SELECT statement
   because auto-commit is on depends on the driver.
   It’s safest to assume a commit occurs here. */
 
/* Prepare the UPDATE statement for multiple executions */
strcpy(sqlStatement,
       "UPDATE employees SET salary = salary * 1.05 WHERE id = ?");
rc = SQLPrepare((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Bind parameter */
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT,
                      SQL_C_LONG, SQL_INTEGER, 10, 0,
                      &index, sizeof(index), NULL);
 
for (index = 0; index < 10; index++) {
 
   /* Execute the UPDATE statement for each
      value of index between 0 and 9 */
   rc = SQLExecute (hstmt);
 
/* Because auto-commit is on, a commit occurs each time
   through loop for a total of 10 commits */
}
 
/* Reset parameter bindings */
rc = SQLFreeStmt((SQLHSTMT)hstmt, SQL_RESET_PARAMS);
 
/* Execute a SELECT statement. A prepare is unnecessary
   because it’s only executed once. */
strcpy(sqlStatement, "SELECT id, name, salary FROM employees");
rc = SQLExecDirect((SQLHSTMT)hstmt, sqlStatement, SQL_NTS);
 
/* Fetch the first row */
rc = SQLFetch(hstmt);
while (rc != SQL_NO_DATA_FOUND) {
 
/* All rows are returned when fetch returns SQL_NO_DATA_FOUND */
 
   /* Get the data for each column in the result set row */
   rc = SQLGetData (hstmt, 1, SQL_C_LONG, &id,
                    sizeof(id), NULL);
   rc = SQLGetData (hstmt, 2, SQL_C_CHAR, &name,
                    sizeof(name), NULL);
   rc = SQLGetData (hstmt,3,SQL_C_LONG,&salary,
                    sizeof(salary), NULL);
   printf("\nID: %d Name: %s Salary: %d", id, name, salary);
 
   /* Fetch the next row of data */
   rc = SQLFetch(hstmt);
}
 
/* Close the cursor */
rc = SQLFreeStmt((SQLHSTMT)hstmt, SQL_CLOSE);
 
/* Whether a commit occurs after a SELECT statement
   because auto-commit is on depends on the driver.
   It’s safest to assume a commit occurs here. */



Like what you see? Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis

Book Content Copyright © 2009 by Prentice Hall PTR. All rights reserved. | Corporate Sponsor DataDirect Technologies.