user1050619
user1050619

Reputation: 20886

Basic logic on Python OOP

I have studied Python and understand how the OOP concept work. The doubt I have is how to implement in a application that has sql interaction..

Assume I have a employee table in SQL which contains Emp name, Emp address , Emp Salary..Now, I need to give a salary raise for all the employees for which I create a class with a method.

My Database logic

db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

cursor = db.cursor()

sql = "Select * from Emp"
cursor.execute(sql)
results = cursor.fetchall()

for row in results:
     name = row[0]
     address = row[1]
     salary  = row[2]



db.close()

Class definiton:

   Class Emp():
      def __init__(self,name,salary):
            self.name = name
            self.salary = salary
      def giveraise(self):
            self.salary = self.salary*1.4

How do I create the object with the details fetched from the Employee table...I know you don't need to create a class to perform this small operation..but Im thinking in the lines of practical implementation.

I need to process record by record.

Upvotes: 0

Views: 203

Answers (2)

Mark Hildreth
Mark Hildreth

Reputation: 43091

As Abe has mentioned, there are libraries that do this sort of mapping for you already (such as SQLAlchmy or Django's included ORM).

One thing to keep in mind though is that a "practical implementation" is subjective. For a small application and a developer with no experience with ORMs, coming up to speed on the general idea of an ORM can add time to the project, and subtle errors to the code. Too many times will someone be confused why something like this takes forever with an orm...

for employee in database.query(Employee).all():
    employee.give_raise(0.5)

Assuming a table with 100 employees, this could make anything between a few and 200+ individual calls to the database, depending on how you've set up your ORM.

It may be completely justifiable to not use OOP, or at least not in the way you've described. This is a completely valid object:

class Workforce(object):
    def __init__(self, sql_connection):
        self.sql_connection = sql_connection

    def give_raise(self, employee_id, raise_amount):
        sql = 'UPDATE Employee SET Salary=Salary + ? WHERE ID = ?'
        self.sql_connection.execute(sql, raise_amount, employee_id)

By no means am I trying to knock on ORMs or dissuade you from using them. Especially if you're just experimenting for the heck of it, learning how to use them properly can make them valuable tools. Just keep in mind that they CAN be a hassle, especially if you're never used one before, and to consider that there are simpler solutions if your application is small enough.

Upvotes: 0

Abe Karplus
Abe Karplus

Reputation: 8550

It sounds like you are looking for an Object-Relational Mapper (ORM). This is a way of automatically integrating an SQL database with an object-oriented representation of the data, and sounds perfect for your use case. A list of ORMs is at http://wiki.python.org/moin/HigherLevelDatabaseProgramming. Once you have the ORM set up, you can just call the giveraise method on all the Emp objects. (Incidentally, the giveraise function you have defined is not valid Python.)

Upvotes: 5

Related Questions