Python supports using large objects (LOB) datatypes — text and image.
Constructor:
Lob(type, obj) – creates an object holding a LOB value.
type – an type of the LOB object. It can have values TEXT or IMAGE as specified below.
obj – a Python buffer object. It is any object which supports the Buffer Protocol. Such objects include the built-in bytearray.
Types
TEXT type – describes text columns in the database.
IMAGE type – describes image columns in the database.
LOB Object Methods
For a bulk copy in operation, the application must use the LOB() constructor to mark a Python object for transfer for a text or image column.
conn = sybpydb.connect(dsn="user=sa;bulklogin=true;chainxacts=off") cur = conn.cursor()cur.execute("create table mytable (id int, t text, i image)") cur.close() blk = conn.blkcursor() blk.copy("mytable", direction="in") # Transfer text and image data using a bytearray. arr1 = bytearray(b"hello this is some text data") lob1 = sybpydb.Lob(sybpydb.TEXT, arr1) arr2 = bytearray(b"hello this is some image data") lob2 = sybpydb.Lob(sybpydb.IMAGE, arr2) blk.rowxfer([1, lob1, lob2])
# Transfer data from a file using memory maps. fh1 = open(“file1”, "rb") mp1 = mmap.mmap(fh1.fileno(), 0 , access=mmap.ACCESS_READ) arr1 = bytearray(mp1) lob1 = sybpydb.Lob(sybpydb.TEXT, arr1) fh2 = open(“file2”, "rb") mp2 = mmap.mmap(fh2.fileno(), 0 , access=mmap.ACCESS_READ) arr2 = bytearray(mp2) lob2 = sybpydb.Lob(sybpydb.IMAGE, arr2) blk.rowxfer([2, lob1, lob2])
For a bulk copy out operation of text and image columns, the text and image columns being transferred must reside at the end of a row.
Data for the text and image columns is returned as a LOB object. The rows from the table must be transferred one by one. After each row is transferred, the data in each LOB object in the row must be retrieved. The readinto() method must be repeatedly called until it returns the None object to indicate that a complete column value has been copied.
# Method to read data from a lob object def getlobdata(lob): outarr = bytearray() chunk = bytearray(1024) while True: len = lob.readinto(chunk); if (len == None): break outarr.extend(chunk[:len]) return outarr blk.copy("mytable", direction="out") # The rows should be transferred one by one. row = blk.rowxfer() print(row[0]) # Now read the lob data for the text column column arr1 = getlobdata(row[1]) print(arr1.decode()) # Now read the lob data for the text column column arr2 = getlobdata(row[2]) print(arr2.decode())