Batch Processing
The CData ODBC Driver for Google Sheets offers bulk load support in Google Sheets through the ODBC Batch API. The driver can execute related SQL data manipulation statements simultaneously by translating them into a single bulk API request. In your application's code, the driver executes statements based on an array of inputs and a parameterized query.
Batch Update Procedure
Complete the following steps to execute a batch update:
- Define arrays of column values for each parameter in the statement.
- Set the SQL_ATTR_PARAMSET_SIZE statement attribute.
- Bind each array to each parameter.
- Execute the parameterized statement.
Bulk Insert
The following code shows how to execute a bulk insert with SQLSetStmtAttr, SQLBindParameter, and SQLExecDirect:
SQLHSTMT pHstmt = NULL;
const int MAX_INSERT_COUNT = 2;
const int MAX_BUFFER_SIZE = 100;
char Column1Array[MAX_INSERT_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbColumn1Array[MAX_INSERT_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_INSERT_COUNT, 0);
char *Column11 = "Jon Doe", *Column12 = "John";
cbColumn1Array[0] = strlen(Column11);
cbColumn1Array[1] = strlen(Column12);
memcpy(Column1Array[0], Column11, strlen(Column11));
memcpy(Column1Array[1], Column12, strlen(Column12));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, Column1Array, MAX_BUFFER_SIZE, cbColumn1Array);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "INSERT INTO Spreadsheet1_Sheet1 (Column1) VALUES (?)", SQL_NTS);
To retrieve the new records' Ids, query the LastResultInfo table:
SQLExecDirect(hstmt, (SQLTCHAR*)"SELECT * FROM LastResultInfo#TEMP", SQL_NTS);
SQLTCHAR sId[20] = {0};
SQLLEN cbId = 0;
SQLBindCol(hstmt, 1, SQL_C_CHAR, sId, 20, &cbId);
SQLRETURN retcode = SQLFetch(hstmt);
Bulk Update
An array of the primary keys of the records to update must be specified. The following code shows how to execute a bulk update with SQLSetStmtAttr, SQLBindParameter, and SQLExecDirect:
SQLHSTMT pHstmt = NULL;
const int MAX_UPDATE_COUNT = 2;
const int MAX_BUFFER_SIZE = 100;
char IdArray[MAX_UPDATE_COUNT][MAX_BUFFER_SIZE] = { 0 };
char IdArray[MAX_UPDATE_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbIdArray[MAX_UPDATE_COUNT] = { 0 };
SQLLEN cbIdArray[MAX_UPDATE_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_UPDATE_COUNT, 0);
char *Column11 = "Jon Doe", *Column12 = "John";
char *Id1 = "Id1", *Id2 = "Id2";
cbColumn1Array[0] = strlen(Column11);
cbColumn1Array[1] = strlen(Column12);
cbIdArray[0] = strlen(Id1);
cbIdArray[1] = strlen(Id2);
memcpy(Column1Array[0], Column11, strlen(Column11));
memcpy(Column1Array[1], Column12, strlen(Column12));
memcpy(IdArray[0], Id1, strlen(Id1));
memcpy(IdArray[1], Id2, strlen(Id2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, Column1Array, MAX_BUFFER_SIZE, cbColumn1Array);
retcode = SQLBindParameter(pHstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, IdArray, MAX_BUFFER_SIZE, cbIdArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "UPDATE Spreadsheet1_Sheet1 SET Column1 = ? WHERE Id = ?", SQL_NTS);
Bulk Delete
An array of the primary keys of the records to delete must be specified. The following code shows how to execute a bulk delete with SQLSetStmtAttr, SQLBindParameter, and SQLExecDirect:
SQLHSTMT pHstmt = NULL;
const int MAX_DELETE_COUNT = 2;
const int MAX_BUFFER_SIZE = 100;
char IdArray[MAX_DELETE_COUNT][MAX_BUFFER_SIZE] = { 0 };
SQLLEN cbIdArray[MAX_DELETE_COUNT] = { 0 };
...
retcode = SQLSetStmtAttr(pHstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)MAX_DELETE_COUNT, 0);
char *Id1 = "Id1", *Id2 = "Id2";
cbIdArray[0] = strlen(Id1);
cbIdArray[1] = strlen(Id2);
memcpy(IdArray[0], Id1, strlen(Id1));
memcpy(IdArray[1], Id2, strlen(Id2));
retcode = SQLBindParameter(pHstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 0, 0, IdArray, MAX_BUFFER_SIZE, cbIdArray);
retcode = SQLExecDirect(pHstmt, (SQLCHAR*) "DELETE FROM Spreadsheet1_Sheet1 WHERE Id = ?", SQL_NTS);