Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question