Detailed Procedure
- Configure a connection.
- Set the connection timeout interval.
To manage the timeout interval (in seconds) for clients to connect to the server, adjust the SQL_LOGIN_TIMEOUT parameter in the SQLSetConnectAttr function. This parameter corresponds to the libpq parameter connect_timeout. A default value of 0 indicates that the parameter is not in effect. You are advised to set it based on the actual network conditions.
SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
- Disable the autocommit option in order to use transactions for commit or rollback.
To use transactions for commit or rollback, disable autocommit by setting SQL_AUTOCOMMIT_OFF in the SQLSetConnectAttr function.
SQLSetConnectAttr(hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, 0);
- Establish a database connection.
Establish a database connection through the SQLConnect function. Below is the function prototype:
1 2 3 4 5 6 7
SQLRETURN SQLConnect(SQLHDBC ConnectionHandle, SQLCHAR *ServerName, SQLSMALLINT NameLength1, SQLCHAR *UserName, SQLSMALLINT NameLength2, SQLCHAR *Authentication, SQLSMALLINT NameLength3);
If ServerName of the data source was set to gaussdb in Preparations, ODBC automatically obtains connection parameters from the odbc.ini file (in Linux environments) or from the data source manager (in Windows environments).
After obtaining the data source, the function utilizes the connection handle hdbc to access all details about the connected data source, including program running status, transaction processing status, and error information. Subsequently, the function employs the appropriate parameters to connect to the database.
SQLConnect(hdbc, (SQLCHAR *)"gaussdb", SQL_NTS, (SQLCHAR *)"", 0, (SQLCHAR *)"", 0);
- Set the connection timeout interval.
- Set batch binding parameters.
- Set batch binding parameters.
Set the total number of rows in the batch binding parameter array. The batchCount variable indicates the total number of rows to be inserted in batches.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)batchCount, sizeof(batchCount));
Set the number of processed rows. The processRows variable indicates the number of rows that have been inserted in batches.
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, (SQLPOINTER)&processRows, sizeof(processRows));
- Prepare statements and bind parameters.
Use the SQLPrepare function to prepare SQL statements. Use the SQLBindParameter function to bind parameters to the prepared statements. The sql variable holds the SQL statement string. SQL_NTS indicates that the string ends with a null character. ids and cols correspond to the two arrays in the id column (INT type) and col column (VARCHAR type).
SQLPrepare(hstmt, (SQLCHAR *)sql, SQL_NTS); SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, sizeof(ids[0]), 0, &(ids[0]), 0, bufLenIds); SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 50, 50, cols, 50, bufLenCols);
- Set batch binding parameters.
- Execute batch insertion.
- Execute batch insertion.
Use the SQLExecute function to execute the prepared SQL statements for batch insertion. The return value of retcode indicates the result of the insertion.
retcode = SQLExecute(hstmt);
- Manually commit or roll back the transaction.
If retcode returns SQL_SUCCESS or SQL_SUCCESS_WITH_INFO, the insertion was successful. In this case, call the SQLEndTran function to commit the transaction. However, if retcode returns any other value, the insertion has failed. In this case, call the SQLEndTran function to roll back the transaction.
SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_COMMIT); // Commit the transaction. SQLEndTran(SQL_HANDLE_DBC, hdbc, SQL_ROLLBACK); // Roll back the transaction.
- Obtain the number of rows processed in batches.
Use the SQLRowCount function to obtain the number of rows actually inserted and store the number in the rowsCount variable.
SQLRowCount(hstmt, &rowsCount);
- Execute batch insertion.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot