Python, Oracle and LOB objects
The Python programming language is my preferred tool to develop quick and (sometimes) dirty scripts to perform single shot data processing tasks.Recently, one of these tasks involved querying a table with a LOB column that stores XML strings. So the challenge was to read the LOB column and parse as string to dump the records to file.
My setup is Python 2.7 with the CxOracle 5.0.4 library. The first try I ran into the “LOB variable no longer valid after subsequent fetch” exception. According to the documentation this is because internally, Oracle uses LOB locators which are allocated based on the cursor array size. Thus, it is important that the data in the LOB object be manipulated before another internal fetch takes place. It also states that the safest way to do this is to use the cursor as an iterator.
Well, that’s all very nice, but I wouldn’t hang a cursor on a whole table, fetching the records one-by-one and processing the whole XML before going for another fetch. That’s not advisable on an OLTP database.
My solution and suggestion to other python users is to perform a two step fetch process:
- Fetch all relevant primary keys from the table;
- Fetch the LOB column one-by-one using the PK list from the previous step.
def executaQueryLOB(txtQuery): import cx_Oracle orcl = cx_Oracle.connect('yourConnectioString') curs = orcl.cursor() curs.execute(txtQuery) result=curs.fetchall() strLOB=result[0][0].read() orcl.close() return strLOBHTH.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment