如何从OracleCx类扩展游标_

2024-09-29 23:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python2.7.12和包cx_Oracle我试图创建一个扩展类,包称之为OracleCursor。我只想从超类继承方法并用一些自定义方法进行扩展。在

首先,我通过

import cx_Oracle

conn = cx_Oracle.connect(username, password, dsn)
cursor = conn.cursor()

然后我有以下内容

^{pr2}$

人们会认为它是通过

class ExtendedCursor(cx_Oracle.Cursor):

    def hello_world(self):
        print('Hello world')


extended = ExtendedCursor(cursor)

但我得到TypeError: argument 1 must be cx_Oracle.Connection, not OracleCursor。对我来说,这个错误没有意义。另外,我不能使用OracleCursor作为我的超类,因为它没有被识别为一个类。在


Tags: 方法importworldconnectusernamepasswordconncursor
1条回答
网友
1楼 · 发布于 2024-09-29 23:29:40

从Connection对象返回游标。您需要创建一个返回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()

退货:

^{pr2}$

相关问题 更多 >

    热门问题