U
U
UserTypical32021-03-28 23:35:33
Python
UserTypical3, 2021-03-28 23:35:33

How to get data from database sequentially?

Hello!
I have such a task before me.
I have a database with 3 columns Student, Balance, WeekSalary
In the database, let's say 10 users.

I need a function to get one of each student (Student) then his balance (Balance) then his salary for the week (WeekSalary) and make changes to his balance in the Balance + WeekSalary database I sketched
the code, but nothing works. Help me please.

db = sqlite3.connect('base.db') # подключаю бд
cur = db.cursor()
def add(db, cur):
    cur.execute('SELECT Name FROM users')
    for Name in cur:
        cur.execute('SELECT Balance FROM users WHERE name = "{}"'.format(Name)) # получаю баланс по имени
        Balance = Name[0] # сохраняю баланс для того чтобы потом прибавить к нему недельную выручку(зарплату)
        for Balance in cur:
            cur.execute('SELECT WeekSalary FROM users WHERE name = "{}"'.format(Name)) # получаю недельную зарплату
            WeekSalary = Balance[0] # сохраняю недельную выручку
            summa = Balance + WeekSalary # складываю 
            cur.execute('UPDATE users SET Balance = {} WHERE Name = {}'.format(summa, Name)) # изменяю баланс
    db.commit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-03-28
@UserTypical3

If I understand everything correctly, it should help:

db = sqlite3.connect('base.db') # подключаю бд
cur = db.cursor()


def add():
    for row in cur.execute('SELECT * FROM users'):
        Name = row['Name']
        Balance = row['Balance']
        WeekSalary = row['WeekSalary']
        summa = Balance+WeekSalary
        cur.execute("UPDATE users SET Balance = {} WHERE Name = {}".format(summa, Name))
        db.commit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question