我在Python中使用類實例時遇到了問題。 我創建了一個新的類ora,它繼承了cx_Oracle包中的connect類。 當我嘗試TU運行此代碼我recive信息Python中的類實例
文件 「pyt.py」,第12行,在更改爲MyQuery ora.myConnect.cursor() AttributeError的: 'NoneType' 對象沒有屬性 '遊標'
因此Python cannote認識到在ora.myConnect中存儲了對實例的引用。 我不知道t know what can be reason of this error and what it
錯誤代碼。
from cx_Oracle import connect
class ora(connect):
myConnect = None
def __init__(self,connstr):
ora.myConnect = connect.__init__(self,connstr)
def myquery(self):
ora.myConnect.cursor()
ora.myConnect.cursor.execute("SELECT * FROM table")
ora.myConnect.cursor.close()
connstr = 'user/[email protected]:port/sid'
connection = ora(connstr)
connection.myquery()
connection.close()
EDIT
我ve tried to replace ora to self but still Python don
噸訪問實例
from cx_Oracle import connect
class ora(connect):
myConnect = None
def __init__(self,connstr):
self.myConnect = connect.__init__(self,connstr)
def myquery(self):
self.myConnect.cursor()
self.myConnect.cursor.execute("SELECT * FROM table")
self.myConnect.cursor.close()
錯誤: self.myConnect.cursor() AttributeError的: 'NoneType' 對象沒有屬性 '遊標'
EDIT2 這段代碼沒有OOP,對我來說self.myConnect sholud refere NCE到對象實例和該對象應包含法光標()
import cx_oracle
connstr = 'user/[email protected]:port/sid'
connection = cx_oracle.connect(connstr)
cursor = connection.cursor()
cursor.execute("SELECT * FROM table")
cursor.close()
connection.close()
'self.myConnect = connect .__ init __(self,connstr)'很奇怪。 '__init__'方法似乎不應該返回一個遊標。你確定你明白你正在擴展的班級應該如何工作? – beerbajay 2012-03-13 23:11:28
根據[這裏的文檔](http://cx-oracle.sourceforge.net/html/module.html#cx_Oracle.connect)我會說你不應該像你所做的那樣擴展'connect'。相反,只需從'__init__'調用'cx_Oracle.connect()'並將連接保存爲'self.myConnect'。 – beerbajay 2012-03-13 23:24:21
self.myConnect sholud例如返回參照對象實例,而不 OOP此代碼的工作 進口cx_oracle connstr = '用戶/ passwd中@主機:端口/ SID' 連接= cx_oracle.connect(connstr) 光標=連接。遊標() 遊標。執行(「SELECT * FROM table」) cursor.close() connection.close() – browarq 2012-03-13 23:29:00