Skip to content Skip to sidebar Skip to footer

How To Extend OracleCursor Class From Cx_Oracle

Using Python 2.7.12 and package cx_Oracle I'm trying to create an extended class of the what the package call OracleCursor. I simply want to inherit the methods from the superclass

Solution 1:

The cursor is returned from the Connection object. You need to create a custom connection that returns your ExtendedCursor.

import cx_Oracle as cxo

class MyCursor(cxo.Cursor):
    def helloWorld(self):
        print "helloWorld"

class MyConnection(cxo.Connection):
    def cursor(self):
        return MyCursor(self)



if __name__ == '__main__':
    conStr = '<user>/<password>@127.0.0.1:1521/xe'
    db = MyConnection(conStr)
    c = db.cursor()

    print c

    c.execute('select 1+1 from dual')
    print(c.fetchall())

    c.helloWorld()

returns:

<__main__.MyCursor on <__main__.MyConnection to ...@127.0.0.1:1521/xe>>
[(2,)]
helloWorld

Post a Comment for "How To Extend OracleCursor Class From Cx_Oracle"