W
W
Winfell2022-03-13 13:21:33
Python
Winfell, 2022-03-13 13:21:33

How to bind in AIOGRAM on InlineKeyboardButton the button to delete a column from SQLITE?

Hello everyone, during the development of the administrator menu (via telegram chat), I thought about the fact that the administrator, although he can enter / add some data, it would be cool if he could delete them as well, and not climb every time SQLite and do everything manually. So, below is the code that I tried to implement this task

@dp.message_handler(text="Удалить из базы данных")
async def sql_del(message: types.Message):
    if message.from_user.id == Admin_Id:
        read = await sqlite_db.sql_read2()
        for ret in read:
            await bot.send_message(message.from_user.id, text=f'{ret[:]}', reply_markup=InlineKeyboardMarkup().
                                   add(InlineKeyboardButton(f'Удалить {ret[:]}', callback_data=f'del {ret[:]}')))


@dp.callback_query_handler(lambda x: x.data and x.data.startswith('del '))
async def del_callback_run(call: types.CallbackQuery):
    await sqlite_db.sql_delete(call.data.replace('del ', ''))
    await call.answer(text=f'{call.data.replace("del ", "")} удалено.', show_alert=True)

here are my "buttons" when pressed, I kind of create a dynamic inline keyboard, where it is possible to delete the data that I removed from the ranking, however, when I call the sql_delete function (I give it below)
async def sql_delete(data):
    cur.execute('DELETE FROM menu WHERE variable ==  ? ', (data,))
    base.commit()

nothing is deleted in the end ... As if everything "seems" to work out and create perfectly (the keyboard and show_alert work out) and there are no errors in the console either, but I don't understand why it's impossible to delete the data.
On the Internet, I found such options with deletions:
'DELETE FROM menu WHERE variable=(?)'
and this option
DELETE FROM menu
WHERE variable= 'data';

in general, I don’t understand / see that I did it wrong. I would be very grateful if you point out my mistake (s).
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2022-03-13
@phaggi

In my opinion it should be something like this:

async def sql_delete(data):
    cur.execute('DELETE FROM menu WHERE variable = ?;', (data,))
    base.commit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question