Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / February 2006

Tip: Looking for answers? Try searching our database.

ODBC - Bulk insert using column binding

Thread view: 
hajaansh@googlemail.com - 21 Feb 2006 09:47 GMT
Hi,

I want to do Bulk inserts using ODBC. Is it possibe to use column wise
binding? The only examples I have found that do bulk inserts use row
binding.

Perhaps someone has an example in C(that works)?

Cheers,

Haja
hajaansh@googlemail.com - 21 Feb 2006 14:41 GMT
Well i have got a half working version. It does do a bulk add, but only
the first row twice. Can anyone see where I went wrong?

SQLUINTEGER    OrderIDArray[3], NumRowsFetched;
SQLCHAR        SalesPersonArray[3][11],
              StatusArray[3][7];
SQLINTEGER     OrderIDIndArray[3],
              SalesPersonLenOrIndArray[3],
              StatusLenOrIndArray[3];
SQLUSMALLINT   RowStatusArray[3], i;
SQLHSTMT       hstmt;

   int loop;
   HDBC hdbc = (HDBC) __externalAddressVal(__INST(parentHandle));
   SQLRETURN sr;

   sr = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
   if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO)
           printf( "Error in allocating statement in
OnViewBulkinsert");

SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_BIND_TYPE, SQL_BIND_BY_COLUMN, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (void *)2, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_ROW_STATUS_PTR, RowStatusArray, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_ROWS_FETCHED_PTR, &NumRowsFetched, 0);
// Set the cursor type.
sr = SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE, (void*)
SQL_CURSOR_DYNAMIC, SQL_IS_UINTEGER);
// Lock out other users.
sr = SQLSetStmtAttr(hstmt, SQL_ATTR_CONCURRENCY,
             (void*) SQL_CONCUR_LOCK, SQL_IS_UINTEGER);
      if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO)
              printf( "Error in setting locking in
OnViewBulkinsert");

// Bind arrays to the OrderID, SalesPerson, and Status columns.
SQLBindCol(hstmt, 1, SQL_C_ULONG, OrderIDArray, 0, OrderIDIndArray);
SQLBindCol(hstmt, 2, SQL_C_CHAR, SalesPersonArray,
sizeof(SalesPersonArray[0]),
           SalesPersonLenOrIndArray);
SQLBindCol(hstmt, 3, SQL_C_CHAR, StatusArray, sizeof(StatusArray[0]),
           StatusLenOrIndArray);

// Execute a statement to retrieve rows from the Orders table.
SQLExecDirect(hstmt, "SELECT OrderID, SalesPerson, Status FROM Orders",
SQL_NTS);

   OrderIDArray[0] = 1;
   OrderIDArray[1] = 2;
   OrderIDIndArray[0] = 0;
   OrderIDIndArray[1] = 0;
   strcpy(SalesPersonArray[0], "Jackson");
   strcpy(SalesPersonArray[1], "Clinton");

   SalesPersonLenOrIndArray[0]= 7;
   SalesPersonLenOrIndArray[1]= 7;

   strcpy(StatusArray[0], "a");
   strcpy(StatusArray[1], "b");

   StatusLenOrIndArray[0] = 1;
   StatusLenOrIndArray[1] = 1;

printf("Do bulk operation");
       sr = SQLBulkOperations(hstmt, SQL_ADD);

       if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO)
               printf( "Error in bulk insert");

// Close the cursor.
SQLCloseCursor(hstmt);
hajaansh@googlemail.com - 21 Feb 2006 15:06 GMT
I should also say that I was working with mysql.

This code works with Access.

I suppose I have to test this with a real database like Oracle now...
sigh


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.