LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2004, 03:28 PM   #1
sleepymish
LQ Newbie
 
Registered: Oct 2003
Posts: 16

Rep: Reputation: 0
ODBC Programming Question with SQLBulkOperations


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/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
a little programming question haora Programming 3 06-24-2005 08:19 PM
ODBC sql.h question javakid Programming 0 11-16-2004 08:34 AM
Question about programming jcramer Mandriva 2 02-01-2004 02:22 PM
MySQL + ODBC + C programming qusestion sleepymish Programming 0 01-13-2004 09:52 AM
Programming question pk21 Linux - Newbie 6 12-02-2002 01:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration