User Tools

Site Tools


python:database:cx_oracle:howto1

How to use ref cursors in cx_Oracle

cx_Oracle is the python programming's oracle database module. It needs oracle client to connect to oracle database. It follows the python's database API specifications. I shared a very small code snippet below about how to use oracle plsql ref cursor type using the cx_Oracle.

example1.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cx_Oracle as cxo
conn=cxo.connect('<username>/<password>@<host>/<database sid>')
cursor=conn.cursor()
temp_cursor=conn.cursor()
cursor.execute('''
DECLARE
TYPE cursor_type IS REF CURSOR;
     PROCEDURE myproc(mycursor OUT cursor_type) IS
       BEGIN
         OPEN mycursor FOR 
         SELECT * FROM <any table or view>;
       END;
BEGIN    
    myproc(:mycursor);
END;''',{"mycursor": temp_cursor})
for row in temp_cursor:
    print row
cursor.close()
temp_cursor.close()
conn.close()

Discussion

Enter your comment. Wiki syntax is allowed:
 
python/database/cx_oracle/howto1.txt · Last modified: 2015/04/19 12:06 (external edit)