sqlite3 не прибавляет новое значение (aiogram)

Рейтинг: 1Ответов: 0Опубликовано: 14.02.2023

Код БД:

import sqlite3


class Database:

    def __init__(self, db_file):
        self.connection = sqlite3.connect(db_file)
        self.cursor = self.connection.cursor()

    def add_user(self, user_id):
        with self.connection:
            result = self.cursor.execute("""INSERT or IGNORE into "users" ("user_id") VALUES (?)""", (user_id,))
            self.connection.commit()
            return result

    def user_exists(self, user_id):
        with self.connection:
            result = self.cursor.execute("""SELECT * FROM "users" WHERE "user_id" = ?""", (user_id,)).fetchall()
            self.connection.commit()
            return bool(len(result))

    def add_balance(self, user_id, balance):
        with self.connection:
            result =  self.cursor.execute("""UPDATE "users" SET "balance" = "balance" + ? WHERE "user_id" = ?""",
                                        (balance, user_id,))
            self.connection.commit()
            return result


    def check_balance(self, user_id):
        with self.connection:
            result =  self.cursor.execute("""SELECT "balance" FROM "users" WHERE "user_id" = ?""", (user_id,)).fetchall()
            self.connection.commit()
            return result

    def check_count(self, user_id):
        with self.connection:
            result =  self.cursor.execute("""SELECT "count" FROM "users" WHERE "user_id" = ?""", (user_id,)).fetchall()
            self.connection.commit()
            return result

    def add_bill(self, user_id, bill_id, amount):
        with self.connection:
            result =  self.cursor.execute("""INSERT into "bills" ("user_id", "bill_id", "amount") VALUES (?, ?, ?)""",
                                        (user_id, bill_id, amount,))
            self.connection.commit()
            return result


    def check_amount(self, bill_id):
        with self.connection:
            result =  self.cursor.execute("""SELECT "amount" FROM "bills" WHERE "bill_id" = ?""", (bill_id,)).fetchall()
            self.connection.commit()
            return result

Хэндлеры:

@dp.message_handler(state=UserStates.sum_order)
async def check_payment(message: types.Message, state: FSMContext):
    amount = int(message.text)
    await state.update_data(
        {
            'amount': amount
        }
    )
    url = crystalpay(amount)
    bill_id = str(url.split('=')[1])
    db.add_bill(message.from_user.id, bill_id, amount)
    await message.answer('<b>Бот зарезервировал для вас ссылку для оплаты!</b>', reply_markup=markups.verify_crypto(bill_id=bill_id, url=url))
    await state.finish()

@dp.callback_query_handler(Text(startswith='verif_'))
async def check_payment2(callback: types.CallbackQuery):
    bill_id = callback.data.split('_', maxsplit=1)[1]
    amount = db.check_amount(bill_id)[0][0]

    if crystalpay_check(bill_id) == 'payed':
        db.add_balance(callback.message.from_user.id, amount)
        await callback.message.answer('<b>Счет был успешно пополнен!</b>')

    elif crystalpay_check(bill_id) == 'notpayed':
        await callback.message.answer('<b>Платеж не был найден!</b>', reply_markup=markups.verify_crypto(False, bill_id))

    elif crystalpay_check(bill_id) == 'processing':
        await callback.message.answer('<b>Платеж в обработке. Пожалуйста, повторите попытку проверки через несколько минут.</b>', reply_markup=markups.verify_crypto(False, bill_id))

Не прибавляется новое значение в balance по указанному user_id. Как это исправить?

Ответы

Ответов пока нет.