Python error with object orientation

Asked

Viewed 56 times

0

class DBAction(object):
    __conn = None
    __cursor = None
    __id = None

    def __init__(self, arg):        
        self.__id = arg
        self.__conn = sqlite3.connect('clientes.db')
        self.__cursor = conn.cursor()   

    def insert(self):
        sql = ''' INSERT INTO history(id,conversation)
              VALUES(?,?) '''       
        self.__cursor.execute(sql, self.__id, "user : "+srt(self.__id))
        self.__conn.commit()


    def verify(self):                   
        self.__cursor.execute("SELECT * FROM history WHERE id = '"+self.__id+"'")    
        rows = cur.fetchall()    
        for row in rows:
            print(row)

The above class is returning the error:

self.__cursor.execute(sql, self.__id, "user : "+srt(self.__id))
NameError: name 'self' is not defined

And I don’t understand why, can anyone tell me why? I’m new to POO in python

  • eviye use names with prefix of two underscores __ - this use is not for "private attributes" - any text you have said this is dated, and was written by an erroneous impression of the language in the past decade. In practice, it will only give you a headache.

  • I talk in more detail about this use of __ in part: https://answall.com/questions/351309/sobrescrever-property-na-classe-filha/352209#352209

1 answer

0


It wouldn’t be because you’re starting self. __cursor with an undefined value? Where does this Conn come from? It wouldn’t be self. __Conn?

def __init__(self, arg):        
    self.__id = arg
    self.__conn = sqlite3.connect('clientes.db')
    self.__cursor = self.__conn.cursor() 
  • Thank you, it was inattentive of me ;-;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.