0

I have created this employee database for my project and need to convert it to a class (I get more marks for including classes) but I need to iterate through each column (ID, name, jobRole, etc.) and create an object of the Employee class I have created.

This is my code so far:

tableID = c.execute('select employeeID from employeeData')
for r in tableID:
    tableName = c.execute('select employeeName from employeeData')
    for s in tableName:
        tableJobRole = c.execute('select employeeJobRole from employeeData')
        for t in tableJobRole:
            tableYearlySalary = c.execute('select employeeYearlySalary from employeeData')
            for u in tableYearlySalary:
                v = s
                exec(str(v))
                v = Employee(r, s, t, u)

When I run the code in my GUI and click a button, it doesn't do what I want it to do. How would I go about creating a bit of code to iterate through the first row, setting the data in the columns to r, s, t and u, and then going onto the next row for all of them at the same time?

Each of r,s,t,u would equal the first row, then I want all of them to be equal to the next row instead of the first three staying on the first row and u going onto the next row until t is incremented then s.

So I'd want to somehow increment each 'for each in ...' by one at the same time somehow.

1
  • 1
    This question is very unclear. If you're looking for a general solution for representing your database schema in terms of Python classes, the SQLAlchemy ORM library might be of interest to you. Commented Jan 14, 2017 at 16:42

1 Answer 1

1

You can use a single query to return all fields for each row of the table. Try this:

employees = []
c.execute('select employeeID, employeeName, employeeJobRole, employeeYearlySalary from employeeData')
results = cursor.fetchall()
for row in results:
    employees.append(Employee(row[0], row[1], row[2], row[3]))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! I'll try it out and will tell you how i do :)
It worked. I can't seem to be able to insert the code though... When I press enter it just submits the comment. Thanks for you time and help. Appreciate it!

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.