-2

I wrote a bot on aiogram, but it doesn't work the way I need it to. It does not save data to the database from time to time. Checked by the sqlite studio utility. after I wrote the algorithm I confirm it db.commit(), but the data is somehow not saved to the database how to fix it?

def fight(user_id:int):
    for mob_id,hp_mob,damage_mob,power_mob,armor_mob in sql.execute(f"SELECT mob_id,hp_mob,damage_mob,power_mob, armor_mob FROM mob WHERE user_id = '{user_id }'").fetchall():
        for hp,damage,power,armor, in sql.execute(f"SELECT hp, damage, power, armor FROM profile WHERE user_id = '{user_id }'"). fetchall():
            damage = (power_mob -  armor)
            damage_mob = (power - armor_mob)
            hp -= damage_mob
            hp_mob -= damage
            sql.execute(f"UPDATE profile SET hp == '{hp}', damage == '{damage}' WHERE user_id = 'user_id '")
            db.commit()
            sql.execute(f"UPDATE mob SET hp_mob == '{hp_mob}', damage_mob == '{damage_mob}' WHERE user_id = 'user_id '")
            db.commit()
            print(damage)
            print(damage_mob)

1 Answers1

0

You have wrong sqlite requests!

  1. User Id is not provided correctly here - WHERE user_id = 'user_id '"

  2. Don't use string interpolation in this case (it'll work with it, but just don't <3)

What I'd suggest:

#getting the values
sql.execute(
    "SELECT `mob_id` , `hp_mob`, `damage_mob`, `power_mob`, `armor_mob` FROM `mob` WHERE `user_id` = ?",
    (user_id,)).fetchone()

#updating user stats
sql.execute(
    "UPDATE `profile` SET `hp` = ?, `damage` = ? WHERE `user_id` = ?",
    (hp, damage, user_id))

#updating mob stats
sql.execute(
    "UPDATE `mob` SET `hp_mob` = ?, `damage_mob` = ? WHERE `user_id` = ?",
    (hp_mob, damage_mob, user_id))

#and a single commit at the end
db.commit()

Hope I solved it for you

biflé
  • 173
  • 1
  • 8
  • I tried your strings, but alas I get the error parametr are of unsupported type – Матвей Васильев Oct 01 '22 at 10:08
  • Может быть ты кормишь не переменные, а, скажем, списки? Можешь дать полный текст ошибки? В какой из строчек ошибка? – biflé Oct 01 '22 at 20:44