trouselife
trouselife

Reputation: 969

Python class inheritance - how to use this to connect to a mysql database

I am using python 2.7 and I have written a set of python classes in order to upload data to my database. However I am not sure I am completely understanding how to use inheritance from class to class. What I have is a User and Database class - which searches for the users/ databases from a list:

class User(object):

    user_lst = ['user1', 'user2', 'user3']

    def __init__(self, username):
        self.username = username

    def find_user(self):
        if self.username not in self.user_lst:
            print('User not found, script will exit')
            exit()
        else:
            pass


class Database(object):

    db_lst = ['db1', 'db2', 'db3']

    def __init__(self, database):
        self.database = database

    def find_db(self):
        if self.database not in self.user_lst:
            print('Database not found, script will exit')
            exit()
        else:
            pass

I get my values for user and database using raw_input() which returns:

un = 'user1'
db = 'db1'

To instantiate these classes as I understand it, I need to pass these values through the class, at which time I can also call the methods -

User(un).find_user()
Database(db).find_db()

I now want to use a third class to inherit these values in order to connect to the database:

class DatabaseConnect(User, Database):

    def __init__(self):
        User.__init__(self, username)
        Database.__init__(self, database)

    def connect(self, pw):
        try:
            connect = MySQLdb.connect(host = 'localhost', user = self.username, passwd = pw, db = self.database, local_infile = 1)
            cursor = connect.cursor()
            print('Connected to '+self.database)
        except:
            print('Did not connect to database')
            exit()

I then try and connect using:

DatabaseConnect().connect('password1')

However this doesn't work. I have tried to add to my DatabaseConnect class init function:

def __init__(self, username, database):
    User.__init__(self, username)
    Database.__init__(self, database)        

I have also played around with creating object variables from these classes such as:

user_obj = User(un)
user_obj.find_user()

db_obj = Database(db)
db_obj.find_user()

Do I need to create these object variables and then pass them through my DatabaseConnection class - if so do I even need to inherit? This is why I am confused. Say I use these object variables and use this class:

class DatabaseConnect(User, Database):

    def __init__(self, username, database):
        self.username = User.username
        self.database = Database.database

    def connect(self, pw):
        try:
            connect = MySQLdb.connect(host = 'localhost', user = self.username, passwd = pw, db = self.database, local_infile = 1)
            cursor = connect.cursor()
            print('Connected to '+self.database)
        except:
            print('Did not connect to database')
            exit()

and then I instantiate it using:

db_connect = DatabaseConnect(user_obj, db_obj) 

How is this any different from simply using the variables themselves:

db_connect = DatabaseConnect(un, db) 

and why do i have to use:

self.username = User.username

instead of simply:

self.username = username

I am struggling to get my head around this concept so any head would be appreciated. Thanks

Upvotes: 1

Views: 3370

Answers (2)

lapinkoira
lapinkoira

Reputation: 8988

If you are inheriting you dont need to create a User and Database instance. You can just create a DatabaseConnect object:

class DatabaseConnect(User, Database):

    def __init__(self, username, database):
        User.__init__(self, username)
        Database.__init__(self, database)

    def connect(self, pw):
        try:
            connect = MySQLdb.connect(host = 'localhost', user = self.username, passwd = pw, db = self.database, local_infile = 1)
            cursor = connect.cursor()
            print('Connected to '+self.database)
        except:
            print('Did not connect to database')
            exit()


dbConnect = new DatabaseConnect("username", "database")

dbConnect.find_db()
Database not found, script will exit

dbConnect.find_user()
User not found, script will exit

Upvotes: 2

Anthon
Anthon

Reputation: 76614

By doing:

def __init__(self, username, database):
    self.username = User.username
    self.database = Database.database

you are not properly initialising the User and Database instances. You need to call their __init__ function. There are several ways to do so e.g.:

def __init__(self, username, database):
    User.__init__(self, username)
    Database.__init__(self, database)

after doing so you can use self.username, etc

Upvotes: 0

Related Questions