Hi,
I'm wondering what's wrong w/ my code below. When I use SQLBulkOperations to insert multiple rows, the data do not line up correctly. A few strange things that I noticed:
1) taking out the SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER)sizeof(dataStruct), 0); doesn't seem to change the result at all.
2) The data in the database seem to bind vertically instead of horizontally. For ex: id: 540, 6, subcode: 255, 6, data: 0, 6
The second dits are the indicator/length. Somehow in the database it stored as 540, 6, 255, 6, 0, 6 in 6 consecutive rows instead of in the same row.
I hope i'm not confusing anyone. Somebody help please.
Mic
/********************** structure definition ****************/
typedef SQLCHAR UINT8;
typedef SQLUSMALLINT UINT16;
typedef SQLUINTEGER UINT32;
typedef SQLINTEGER INT32;
// data struct
typedef struct{
INT32 FrameID;
SQLINTEGER FrameIDLen;
UINT8 SubCode;
SQLINTEGER SubCodeLen;
UINT16 Data;
SQLINTEGER DataLen;
}dataStruct;
/************* part of bulk insert code ********************/
if(numInserts)
{
SQLRETURN rc;
SQLUSMALLINT RowStatusArray[numInserts];
SQLINTEGER bindOffset = 0;
// test
char test[128];
SQLPOINTER ValuePtr = (SQLPOINTER)test;
SQLINTEGER BufferLength = sizeof(test);
SQLINTEGER StringLength;
printf("stringlength: %d\n", StringLength);
// set statement attributes
SQLSetStmtAttr(m_hStmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_KEYSET_DRIVEN, 0);
SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER)sizeof(dataStruct), 0);
//SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0);
printf("stringlength: %d\n", StringLength);
printf("column: %d\n", SQL_BIND_BY_COLUMN);
printf("datastruct: %d\n", sizeof(dataStruct));
printf("%d\n", SQLGetStmtAttr(m_hStmt, SQL_ATTR_ROW_BIND_TYPE, ValuePtr, BufferLength, &StringLength));
printf("row bind type: %s, %d\n", &test, StringLength);
SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) numInserts, 0);
SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_STATUS_PTR, (SQLPOINTER) RowStatusArray, 0);
SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_BIND_OFFSET_PTR, (SQLPOINTER) &bindOffset, 0);
// execute a statement to retrieve rows from the table
rc=SQLExecDirect(m_hStmt, (SQLCHAR*) "SELECT * FROM data", SQL_NTS);
if(rc == SQL_ERROR)
printf("error\n");
rc = SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)numInserts, 0);
if(rc == SQL_ERROR)
printf("error\n");
// not working yet
SQLBindCol(m_hStmt, 1, SQL_C_SLONG, &DS[0].FrameID, sizeof(DS[0].FrameID), &DS[0].FrameIDLen);
SQLBindCol(m_hStmt, 2, SQL_C_CHAR, NULL, 0, 0);
SQLBindCol(m_hStmt, 3, SQL_C_UTINYINT, &DS[0].SubCode, sizeof(DS[0].SubCode), &DS[0].SubCodeLen);
SQLBindCol(m_hStmt, 4, SQL_C_USHORT, &DS[0].Data, sizeof(DS[0].Data), &DS[0].DataLen);
rc = SQLBulkOperations(m_hStmt, SQL_ADD);
if(rc == SQL_ERROR)
{
printf("data insert error\n");
PrintError();
}
else if(rc == SQL_SUCCESS)
{
printf("data insert successful\n");
}
printf("after\n");
for(int j=0; j printf("%d, ", RowStatusArray[j]);
printf("\n");
// reset
SQLSetStmtAttr(m_hStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)0, 0);
}
/***************** OUTPUT *****************/
| 537 | 1 | 255 | 0 |
| 6 | 152 | 0 | 0 |
| 255 | 22 | 0 | 6 |
| 6 | 153 | 0 | 0 |
| 0 | 66 | 6 | 537 |
| 6 | 154 | 0 | 0 |
| 538 | 1 | 0 | 0 |
| 6 | 155 | 0 | 0 |
| 0 | 67 | 0 | 6 |
| 6 | 156 | 0 | 0 |
| 539 | 1 | 0 | 0 |
| 6 | 157 | 0 | 0 |
| 0 | 68 | 0 | 6 |
| 6 | 158 | 0 | 0 |
| 540 | 1 | 255 | 0 |
| 6 | 159 | 0 | 0 |
| 255 | 23 | 0 | 6 |
| 6 | 160 | 0 | 0 |
| 0 | 69 | 6 | 540 |
| 6 | 161 | 0 | 0 |