2.3 Bulkcopy Objects

his is an extension of the DB-API 2.0 specification.

This object provides an interface to the Sybase bulkcopy functionality.

rowxfer( [data])
If the Bulkcopy object direction is CS_BLK_IN then the sequence passed as the data argument is sent as one row to the server. If the direction is CS_BLK_OUT then one row will be returned from the server. If there are no more rows, None is returned.

batch( )
Marks a complete bulkcopy batch. The number of rows transferred in the batch is returned.

done( )
Marks a complete bulkcopy operation. The number of rows transferred in the batch is returned. done() must be called (or the Bulkcopy object destroyed) to flush any outstanding cached rows to the DB. In addition, any other operation on the associated Connection object will fail until done is called.

__iter__( )
Returns an iterator that will iterate over all the rows in the table returned by the bulkcopy out operation. The Bulkcopy object should be created with the CS_BLK_OUT direction argument. Intermixed use of the iterator returned from __iter__ and the rowxfer to retrieve rows is supported; both use the same underlying function and will return all rows without missing or duplicating any row.

rows( )
An alias for __iter__.

arraysize( T)
he arraysize passed to the constructor. Up to this many rows will be cached in memory and sent to the DB server in one request. Read only.

totalcount( A)
count of the total number of lines passed to/from the DB server. Read only.

An example of using the Bulkcopy class follows:

from Sybase import *

c = connect('server', 'user', 'password', bulkcopy=1, auto_commit=1);

c.execute("Create table #b(a int, b varchar(10), c float)")

b = c.bulkcopy('#b') # CS_BLK_IN is default
for r in range(32):
    b.rowxfer([r, ("xxx%d" % r), r * 0.1])
    if r % 5 == 4:
        ret = b.batch()
        print "post batch, batch was", ret, "count = ", b.totalcount
ret = b.done()
print "post done, last batch was", ret, "count = ", b.totalcount

for r in c.bulkcopy('#b', out=1):
    print r