2012-03-13 2 views
0

Python에서 클래스 인스턴스를 사용하는 데 문제가 있습니다. Ive는 cx_Oracle 패키지에서 연결 클래스를 상속받은 ora 클래스를 새로 만들었습니다. 나는 너와 내가 정보를 받아 봐이 코드를 실행하려고하면Python의 클래스 인스턴스

MYQUERY에서 파일 "pyt.py", 라인 (12), ora.myConnect.cursor() AttributeError : 'NoneType'개체가 어떤 속성 '커서'

이 없습니다

그래서 파이썬은 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() 

편집

나는 t 인스턴스에 액세스 할 수 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 후부는 객체 인스턴스하고 당신이 self을 원하는처럼이 객체 메소드 커서()

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() 
+0

'self.myConnect = connect .__ init __ (self, connstr)'이 이상합니다. '__init__' 메쏘드가 커서를 리턴 할 것 같지는 않습니다. 연장하는 수업이 어떻게 작동해야하는지 이해하셨습니까? – beerbajay

+0

[여기에있는 문서] (http://cx-oracle.sourceforge.net/html/module.html#cx_Oracle.connect)에 기반하여 나는 당신이 한 것처럼'connect'를 실제로 확장한다고 말하지 않을 것입니다. 대신'__init__'에서'cx_Oracle.connect()'를 호출하고 연결을'self.myConnect'로 저장하십시오. 연결 = cx_oracle.connect (connstr) 커서 = 연결 : – beerbajay

+0

self.myConnect이 코드는 수입 cx_Oracle과 connstr = '포트/SID를 사용자 /의 passwd @ 호스트를'작동 OOP하지 않고, 예를 들어, 인스턴스를 객체에 을 참조를 반환 sholud. 커서() 커서.execute ("SELECT * FROM table") cursor.close() connection.close() – browarq

답변

2

것 같다 포함되어야합니다

class ora(connect): 
    myConnect = None 

    def __init__(self, connstr):  
     self.myConnect = connect.__init__(self, connstr) 

    # ... 

ora 클래스의 이름이 아닌 인스턴스입니다.

업데이트 다음을 시도해보십시오 :

from cx_Oracle import connect 

class ora: 
    myConnect = None 

    def __init__(self, connstr):  
     self.myConnect = connect(connstr) 

    def myquery(self): 
     self.myConnect.cursor() 
     self.myConnect.cursor.execute("SELECT * FROM table") 
     self.myConnect.cursor.close() 
+0

'__init__'과'myquery'에서'ora'를'self'로 바꾸면'connection' 객체가 작동해야합니다. 어떤 오류가 발생하고 있습니까? – beerbajay

+0

나는 코멘트가 싫증이났다. 그래서 나는 첫번째 게시물에 더 많은 내용을 추가했다. – browarq

1

가 왜 self.myConnectconnect 인스턴스를 참조 하시겠습니까? 그것은 OOP에 대한 완전한 오해입니다. ora 인스턴스 은 인스턴스 인입니다. self.cursor은 커서를 찾는 곳입니다.

여기에 코드가 보일 것입니다 방법은 다음과 같습니다

어떤 경우
class ora(connect): 

    def __init__(self,connstr):  
    super(ora, self).__init__(connstr) 

    def myquery(self): 
    self.cursor.execute("SELECT * FROM table") 
    self.cursor.close() 

, __init__ 그래서 항상이 None에 구속 당할 것입니다 반환 값에 self.myConnect 설정, 아무것도 반환해서는 안됩니다.

+0

고마워 - 나는이 metod super에 대해 잘 몰랐다. 이제는 효과가있다. – browarq

+0

해결되면 대답을 표시하는 것을 잊지 마라. 너의 문제. –