0

I have problem with using class instance in Python. Ive created a new class ora which inherit connect class from cx_Oracle package. When I try tu run this code I recive information

File "pyt.py", line 12, in myquery ora.myConnect.cursor() AttributeError: 'NoneType' object has no attribute 'cursor'

So Python cannote recognize that in ora.myConnect is stored reference to instance. I dont know what can be reason of this error and what its wrong with code.

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/passwd@host:port/sid' 
connection = ora(connstr)      
connection.myquery()                 
connection.close()

EDIT

Ive tried to replace ora to self but still Python dont have access to instance

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

Error: self.myConnect.cursor() AttributeError: 'NoneType' object has no attribute 'cursor'

EDIT2 This code works without OOP, for me self.myConnect sholud reference to object instance and this object should contain method cursor()

import cx_oracle
connstr = 'user/passwd@host:port/sid' 
connection = cx_oracle.connect(connstr)                   
cursor = connection.cursor()                              
cursor.execute("SELECT * FROM table")
cursor.close()
connection.close()
5
  • self.myConnect = connect.__init__(self,connstr) is odd. It seems unlikely that the __init__ method should return a cursor. Are you sure you understand how the class you're extending is supposed to work? Commented Mar 13, 2012 at 23:11
  • Based on the documentation here I would say you're not really supposed to extend connect as you have done. Instead, just call cx_Oracle.connect() from your __init__ and save the connection as self.myConnect. Commented Mar 13, 2012 at 23:24
  • self.myConnect sholud return reference to object instance, for example without OOP this code works import cx_oracle connstr = 'user/passwd@host:port/sid' connection = cx_oracle.connect(connstr) cursor = connection.cursor() cursor.execute("SELECT * FROM table") cursor.close() connection.close() Commented Mar 13, 2012 at 23:29
  • I've updated my answer below. Commented Mar 13, 2012 at 23:33
  • Thanks @beerbajay Your method without inherit works also great thnks for help Commented Mar 13, 2012 at 23:48

2 Answers 2

2

It seems like you want self:

class ora(connect):
    myConnect = None

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

    # ...

ora is the name of the class, not the instance.

Update Try the following:

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()   
Sign up to request clarification or add additional context in comments.

2 Comments

If you replace ora with self in __init__ and myquery, your connection object should be functional. What error are you getting?
I had a problem with comments fromating, so I`ve added more details in first post
1

Why do you want self.myConnect to refer to the connect instance? That's a complete misunderstanding of OOP. The ora instance is the connect instance. self.cursor is where you find the cursor.

Here's how your code should look:

class ora(connect):

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

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

In any case, __init__ must never return anything, so setting self.myConnect to the return value will always result in it being bound to None.

1 Comment

Thanks - I don`t knew about this metod super, now it works great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.