Python MySQLdb - Connection in a class
Question: I am making a Python project where I have to seek and retreive data from a
database.  
I tried making a class, in which I declare the connection and do my queries,
here is moreless what I have so far.

    
    
    import MySQLdb
    dbc =("localhost","root","1234","users")
    class sql:
        db = MySQLdb.connect(dbc[0],dbc[1],dbc[2],dbc[3])
        cursor = db.cursor()
    
        def query(self,sql):
            sql.cursor.execute(sql)
            return sql.cursor.fetchone()
    
        def rows(self):
            return sql.cursor.rowcount
    
    sqlI = sql()
    print(sqlI.query("SELECT `current_points` FROM `users` WHERE `nick` = 'username';"))
    

So, the main problem is that the variable `db` and `cursor` are not callable
from other def's/functions from the same Class. What I'd like to get, is a
polished query, where I can make queries and retreive it's content. This would
summarize my code, therefore I should do.


Answer: That's not how you write classes in Python. You need to define your connection
and cursor inside the `__init__` method, and refer to them via `self`.

    
    
    class sql:
    
        dbc = ("localhost","root","1234","users")
    
        def __init__(self):
            db = MySQLdb.connect(*self.dbc)
            self.cursor = db.cursor()
    
        def query(self,sql):
            self.cursor.execute(sql)
            return self.cursor.fetchone()
    
        def rows(self):
            return self.cursor.rowcount
    

