Skip to content

Instantly share code, notes, and snippets.

@TerryMooreII
Created September 24, 2012 00:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save TerryMooreII/3773572 to your computer and use it in GitHub Desktop.
Save TerryMooreII/3773572 to your computer and use it in GitHub Desktop.
Get Oracle DBMS_Output from Python's cx_Oracle library
import cx_Oracle
conn = cx_Oracle.Connection("userid/password@tns")
curs = conn.cursor()
curs.callproc("dbms_output.enable")
sqlCode = """
some long
sql code
with dbms_output
"""
curs.execute(sqlCode)
statusVar = curs.var(cx_Oracle.NUMBER)
lineVar = curs.var(cx_Oracle.STRING)
while True:
curs.callproc("dbms_output.get_line", (lineVar, statusVar))
if statusVar.getvalue() != 0:
break
print lineVar.getvalue()
@dlenski
Copy link

dlenski commented May 26, 2016

Very nice. With cx_Oracle v5.2 and Oracle 11g, however, I get a segfault unless I provide an explicit parameter (buffer_size) for dbms_output.enable.

That is:

curs.callproc("dbms_output.enable", (None,)) # or explicit integer size

@jagadeesanm-jag
Copy link

Awesome !!! thanks a lot for this wonderful example . It definitely helps those who are beginners .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment