C language + ODBC + SQL operations (add data to SQL), odbcsql
To save time, I reference the database table in the previous section and the structure array of C language, add data to the structure array, and clear the database data.
Step 1: Use the SQLBindParameter function.
SQLRETURN SQLBindParameter (SQLHSTMT StatementHandle, // statement handle SQLUSMALLINT ParameterNumber, // the number of parameters in the statement. The minimum value is 1 SQLSMALLINT InputOutputType, // input/output parameter type Identification [1] SQLSMALLINT ValueType, // corresponding C data type Identification [2] SQLSMALLINT ParameterType, // The corresponding SQL data type identifier [2] SQLULEN ColumnSize, // The length of the corresponding field SQLSMALLINT DecimalDigits, // if it is a floating point, the corresponding field precision SQLPOINTER ParameterValuePtr, // parameter cache SQLLEN BufferLength, // parameter cache bytes SQLLEN * StrLen_or_IndPtr); // ID used to indicate the string length or NULL value [3]
Note:
1. In ODBC, the C language data type corresponding to the SQL _INTEGER type is SQL _C_LONG rather than SQL _C_INT, and there is no SQL _C_INT type.
2. When saving integer data, SQLPOINTER ParameterValuePtr should add the address character '&'.
3. SQLUSMALLINT ParameterNumber cannot be written at will. It must be in the same order as when creating a table.
Step 2: Write the function for adding data.
Void handle (int I) {SQLRETURN ret; SQLHENV henv; // SQLHANDLE henv SQLHDBC hdbc; // SQLHANDLE hdbc SQLHSTMT hstmt; // SQLHANDLE hstmt ret = SQLAllocHandle (handle, NULL, & henv); // apply for the Environment handle ret = SQLSetEnvAttr (henv, kernel, (SQLPOINTER) SQL _OV_ODBC3, SQL _IS_INTEGER); // set the environment attribute ret = SQLAllocHandle (SQL _HANDLE_DBC, henv, & hdbc); // request the database connection handle ret = SQLConnect (hdbc, (SQLCHAR *) "phonesql", SQL _nt, (SQL CHAR *) "sa", SQL _cnt, (SQLCHAR *) "123456", SQL _cnt); // connect to the database if (ret = SQL _SUCCESS | ret = SQL _SUCCESS_WITH_INFO) {ret = SQLAllocHandle (SQL _HANDLE_STMT, hdbc, & hstmt ); // apply for an SQL statement handle SQLCHAR SQL [] = "INSERT INTO worker VALUES (?,?,?,?,?,?,?,?,?,?); "; Sqlinteger p = SQL _cnt; ret = SQLPrepare (hstmt, SQL, SQL _cnt); ret = SQLBindParameter (hstmt, 1, SQL _PARAM_INPUT, SQL _C_LONG, SQL _INTEGER, 0, & gong [I]. num, 0, & P); // bind the ret = SQLBindParameter (hstmt, 2, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 20, 0, gong [I]. name, 20, & P); // The binding parameter ret = SQLBindParameter (hstmt, 3, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 10, 0, gong [I]. sex, 10, & P); // The binding parameter ret = SQLBindParameter (hstmt, 4, SQL _PARAM_INPUT, SQL _ C_LONG, SQL _INTEGER, 0, 0, & gong [I]. age, 0, & P); // The binding parameter ret = SQLBindParameter (hstmt, 5, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 20, 0, gong [I]. shenfennum, 20, & P); ret = SQLBindParameter (hstmt, 6, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 10, 0, gong [I]. xueli, 10, & P); ret = SQLBindParameter (hstmt, 7, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 10, 0, gong [I]. mianmao, 10, & P); ret = SQLBindParameter (hstmt, 8, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 20, 0, gong [I]. mima, 20, & P); ret = SQLBindParameter (hstmt, 9, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 10, 0, gong [I]. quanxian, 10, & P); ret = SQLBindParameter (hstmt, 10, SQL _PARAM_INPUT, SQL _C_CHAR, SQL _CHAR, 20, 0, gong [I]. born, 20, & P); ret = SQLExecute (hstmt); // directly execute the SQL statement if (ret = SQL _SUCCESS | ret = SQL _SUCCESS_WITH_INFO) {/***************** test struct array ****************** **/printf ("message % d added successfully! \ N ", I + 1); SQLFreeHandle (SQL _HANDLE_STMT, hstmt); // release statement handle} else printf (" failed to add database operation! \ N "); SQLDisconnect (hdbc); // disconnect from the database} else printf (" failed to connect to the database! \ N "); SQLFreeHandle (SQL _HANDLE_DBC, hdbc); // release the connection handle SQLFreeHandle (SQL _HANDLE_ENV, henv); // release the environment handle}
Note: SQLCHAR SQL [] = "INSERT INTO worker VALUES (?,?,?,?,?,?,?,?,?,?); '? 'Indicates the data to be added. (The question mark is used to add data)
Step 3: Write the main function call with the Code omitted.
After completion, the data in the database changes