Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2004
    Posts
    7

    Unanswered: SQLBulkOperations question

    Hi,

    I'm trying to use SQLBulkOperations and this is my code:

    *************************************************
    #include <stdio.h>
    #include <string.h>

    #include "table.h"

    // structure to hold info
    typedef struct{
    // SQLCHAR Bookmark[10];
    // SQLINTEGER BookmarkLen;
    SQLCHAR field1[6];
    SQLINTEGER field1Len;
    SQLCHAR field2[6];
    SQLINTEGER field2Len;
    }calcStruct;


    void main(int argc, char *argv[])
    {

    // create
    cDataTable db((SQLCHAR*) "test");

    SQLCHAR *dsn = (SQLCHAR *) "test";
    SQLCHAR *uid = (SQLCHAR *) "root";
    SQLCHAR *pwd = (SQLCHAR *) "";

    //SQLRETURN rc;

    db.Connect(dsn, uid, pwd);

    // allocate 10 calcStruct
    calcStruct CalcArray[10];
    SQLRETURN rc;
    SQLHSTMT hstmt = db.m_hStmt;
    SQLUSMALLINT RowStatusArray[10];
    SQLINTEGER BindOffset = 0, numInserts=0;


    // set the following attributes
    SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER)
    SQL_CURSOR_KEYSET_DRIVEN, 0);
    SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER)
    sizeof(calcStruct), 0);
    SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) 10, 0);
    SQLSetStmtAttr(hstmt, SQL_ATTR_USE_BOOKMARKS, (SQLPOINTER) SQL_UB_VARIABLE,
    0);
    SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_STATUS_PTR, (SQLPOINTER) RowStatusArray,
    0);
    SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_OFFSET_PTR,
    (SQLPOINTER)&BindOffset, 0);


    // get new calc data
    strcpy((char*)CalcArray[0].field1, "test2");
    CalcArray[0].field1Len=sizeof(CalcArray[0].field1);
    strcpy((char*)CalcArray[0].field2, "test3");
    CalcArray[0].field2Len=sizeof(CalcArray[0].field2);;
    numInserts++;

    strcpy((char*)CalcArray[1].field1, "test4");
    CalcArray[1].field1Len=sizeof(CalcArray[1].field1);
    strcpy((char*)CalcArray[1].field2, "test5");
    CalcArray[1].field2Len=sizeof(CalcArray[1].field2);
    numInserts++;

    strcpy((char*)CalcArray[2].field1, "test6");
    CalcArray[2].field1Len=sizeof(CalcArray[2].field1);
    strcpy((char*)CalcArray[2].field2, "test7");
    CalcArray[2].field2Len=sizeof(CalcArray[2].field2);
    numInserts++;

    // print struct
    int j;
    for(j=0; j<numInserts; j++)
    {
    printf("Element %d - %s, %s\n", j, CalcArray[j].field1,
    CalcArray[j].field2);
    printf("Element %d - %d, %d\n", j, CalcArray[j].field1Len,
    CalcArray[j].field2Len);
    }

    // send data to source
    if(numInserts)
    {
    // execute a statement to retrieve rows from the table
    rc=SQLExecDirect(hstmt, (SQLCHAR*) "SELECT field1, field2 FROM
    calc_table", SQL_NTS);

    rc = SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)
    numInserts, 0);

    // bind arrays
    // rc=SQLBindCol(hstmt, 0, SQL_C_VARBOOKMARK, &CalcArray[0].Bookmark,
    sizeof(CalcArray[0].Bookmark), &CalcArray[0].BookmarkLen);
    rc=SQLBindCol(hstmt, 1, SQL_C_CHAR, &CalcArray[0].field1,
    sizeof(CalcArray[0].field1), &CalcArray[0].field1Len);
    rc=SQLBindCol(hstmt, 2, SQL_C_CHAR, &CalcArray[0].field2,
    sizeof(CalcArray[0].field2), &CalcArray[0].field2Len);
    printf("bind rc:%s\n", rc);

    //BindOffset=sizeof(calcStruct);
    BindOffset=0;

    printf("bindoffset %d\n", BindOffset);
    printf("numInserts %d\n", numInserts);
    rc = SQLBulkOperations(hstmt, SQL_ADD);
    if(rc == SQL_ERROR)
    {
    printf("insert error\n");
    db.PrintError();
    }
    else if(rc == SQL_SUCCESS)
    {
    printf("insert successful\n");
    }
    else if(rc == SQL_SUCCESS_WITH_INFO)
    {
    printf("Insert successful w/ info\n");
    }
    else
    {
    printf("not sure\n");
    }

    int i;
    for(i=0; i<10; i++)
    printf("status array %d\n", RowStatusArray[i]);

    // reset
    SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER)10, 0);
    BindOffset = 0;
    }

    db.Disconnect();

    }

    *************************************************

    This is what show up in my database:

    field1 field2
    ------------------------
    lalala tatata
    test2 test3
    *garbage* *garbage*
    test3 test4


    *garbage is just some random unicode

    Does anyone know what I did wrong?

    Thanks!
    __________________________________________________ _______________
    Learn how to choose, serve, and enjoy wine at Wine @ MSN.
    http://wine.msn.com/

  2. #2
    Join Date
    Jan 2004
    Posts
    7
    Somehow changing the bindcol statements to the following works:


    rc=SQLBindCol(hstmt, 1, SQL_C_CHAR, &CalcArray[0].field1,
    sizeof(calcStruct), &CalcArray[0].field1Len);

    rc=SQLBindCol(hstmt, 2, SQL_C_CHAR, &CalcArray[0].field2,
    sizeof(calcStruct)&CalcArray[0].field2Len);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •