问题描述
我想知道如何使用C ++在ODBC中使用sqlBulkOperations()来插入可以选择的数据,并使用该数据插入数据库中的另一个表中。我试图解决它,但无法做到。搜索时说您可以使用sqlBulkOperations()使用sql_Add属性插入。但这是行不通的。没有任何单个示例可以解释相同的情况。请任何人可以帮助。下面是代码。问题:
-
我想从table1中选择并将其插入table2。是否可以使用sqlBulkOperations(),如果是,那么此程序还有第二部分。如果没有,那是什么呢?
-
我有一些手册数据,例如我稍后将在本节中插入的数据。是否可以使用sqlBulkOperations()插入。
// Some standard headers #include "pch.h" #include <stdio.h> #include <stdlib.h> // Only needed for Windows clients #include <windows.h> // Standard ODBC headers #include <sql.h> #include <sqltypes.h> #include <sqlext.h> #include <cstdio> #include <iostream> #define ROWSET_SIZE 5 using namespace std; int main() { /* declare and initialize local variables */ sqlLEN rowCount; sqlRETURN rc; sqlHENV hdlEnv; rc = sqlAllocHandle(sql_HANDLE_ENV,sql_NULL_HANDLE,&hdlEnv); rc = sqlSetEnvAttr(hdlEnv,sql_ATTR_ODBC_VERSION,(sqlPOINTER)sql_OV_ODBC3,sql_IS_UINTEGER); sqlHDBC hdlDbc; rc = sqlAllocHandle(sql_HANDLE_DBC,hdlEnv,&hdlDbc); rc = sqlConnect(hdlDbc,(sqlCHAR*)"LocalDB",sql_NTS,(sqlCHAR*)"sa",(sqlCHAR*)"abc@123",sql_NTS); sqlHSTMT hstmt; sqlAllocHandle(sql_HANDLE_STMT,hdlDbc,&hstmt); struct rowTag { sqlINTEGER CustID; sqlCHAR CustName[21]; sqlCHAR Phone_Number[21]; sqlINTEGER CustID_L; sqlINTEGER CustName_L; sqlINTEGER Phone_Number_L; }row[5]; sqlUSMALLINT rowStatus[5]; sqlINTEGER BindOffset = 0; /* Set up dynamic cursor type */ //rc = sqlSetStmtAttr(hstmt,sql_ATTR_CURSOR_TYPE,(sqlPOINTER)sql_CURSOR_DYNAMIC,0); ///* Set pointer to row status array */ //rc = sqlSetStmtAttr(hstmt,sql_ATTR_ROW_STATUS_PTR,(sqlPOINTER)rowStatus,0); //rc = sqlSetStmtAttr(hstmt,sql_BIND_TYPE,(void*)(sizeof(row) / 5),0); sqlSetStmtAttr(hstmt,(sqlPOINTER)sql_CURSOR_KEYSET_DRIVEN,0); sqlSetStmtAttr(hstmt,sql_ATTR_ROW_BIND_TYPE,(sqlPOINTER)sizeof(row),0); //sqlSetStmtAttr(hstmt,sql_ATTR_ROW_ARRAY_SIZE,(sqlPOINTER)5,sql_ATTR_USE_BOOKMARKS,(sqlPOINTER)sql_UB_VARIABLE,sql_ATTR_ROW_BIND_OFFSET_PTR,(sqlPOINTER)&BindOffset,0); sqlCHAR sqlstmt[] = "select CustId,CustName,Phone_Number from customers"; /* Execute query */ rc = sqlExecDirect(hstmt,sqlstmt,sql_NTS); /* Call sqlBindCol() for each result set column */ rc = sqlBindCol(hstmt,1,sql_C_LONG,(sqlPOINTER)&row[0].CustID,(sqlINTEGER)sizeof(row[0].CustID),// / ROWSET_SIZE,&row[0].CustID_L); rc = sqlBindCol(hstmt,2,sql_C_CHAR,(sqlPOINTER)&row[0].CustName,(sqlINTEGER)sizeof(row[0].CustName),&row[0].CustName_L); rc = sqlBindCol(hstmt,3,(sqlPOINTER)&row[0].Phone_Number,(sqlINTEGER)sizeof(row[0].Phone_Number),&row[0].Phone_Number_L); /* For each column,place the new data values in */ /* the rgbValue array,and set each length value */ /* in the pcbValue array to be the length of the */ /* corresponding value in the rgbValue array. */ //Fetching Results and displaying the result //while (sql_SUCCEEDED(rc = sqlFetchScroll(hstmt,sql_FETCH_NEXT,1))) { // // Print the bound variables,which Now contain the values from the // // fetched row. // sqlUINTEGER i = 0; // cout << row[i].CustID << " | " << row[i].CustName << " | " << row[i].Phone_Number << endl; // i++; //} strncpy((char*)row[0].CustName,"Gilligan",sizeof(row[0].CustName)); strncpy((char*)row[1].CustName,"Skipper",sizeof(row[1].CustName)); strncpy((char*)row[2].CustName,"Mr. Howe",sizeof(row[2].CustName)); strncpy((char*)row[3].CustName,"Mrs. Howe",sizeof(row[3].CustName)); strncpy((char*)row[4].CustName,"Ginger",sizeof(row[4].CustName)); for (int loop = 0; loop < 5; loop++) { //Five entries for CustID row[loop].CustID = 200 + loop; //Five entries for Phone_Number strcpy((char*)row[loop].Phone_Number,"1-800-900-" + loop); //Specifying length of the CustID column row[loop].CustID_L = sizeof(row[0].CustID); //Specifying length of CustName row[loop].CustName_L = sql_NTS; //Specifying length of Phone_Number row[loop].Phone_Number_L = sql_NTS; } /* Set number of rows to insert */ //rc = sqlSetStmtAttr(hstmt,0); // /* Perform the bulk insert */ rc = sqlBulkOperations(hstmt,sql_ADD); rc = sqlEndTran(sql_HANDLE_DBC,sql_COMMIT); cout << "sqlBulkOperations End\n" << endl; sqlRowCount(hstmt,&rowCount); printf("Rows Effected %i\n",(int)rowCount); /* int i; for (i = 0; i < 5; i++) printf("status array %d\n",rowStatus[i]);*/ }
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)