N
N
NosferatuZodd2022-01-26 21:03:57
Python
NosferatuZodd, 2022-01-26 21:03:57

How to delete data from pysqlite3 table?

I use python and pysqlite3, I want to delete a user by his chat id, code:

Database.py:

import sqlite3

class Database:
    def __init__(self, database_file):
        self.connection = sqlite3.connect(database_file, check_same_thread = False)
        self.cursor = self.connection.cursor()

    def add_queue(self, chat_id):
        with self.connection:
            return self.cursor.execute("INSERT INTO 'queue' ('chat_id') VALUES (?)", (chat_id,))

    def delete_queue(self, chat_id):
        with self.connection:
            return self.cursor.execute("DELETE FROM 'queue' WHERE 'chat_id' = ?", (chat_id,)) # не работает

main.py:
...
@bot.message_handler(content_types = ['text'])
def bot_message(message):
    if message.chat.type == "private":
        if message.text == "Поиск собеседника":
            markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
            item1 = types.KeyboardButton("❌Остановить поиск")
            markup.add(item1)

            db.add_queue(message.chat.id)

            bot.send_message(message.chat.id, " Поиск собеседника", reply_markup = markup)

        elif message.text == "❌Остановить поиск":
            db.delete_queue(message.chat.id) # доходит до этой строки однако из базы данных ничего не удаляет
            bot.send_message(message.chat.id, "❌Поиск остановлен, напишите /menu")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2022-01-26
@Vindicar

When you do with self.connection, you are creating a database transaction - i.e. a connected set of operations that will either be applied collectively or rejected collectively. Since you only have one operation each time, it doesn't make sense.
And here the cursor at you is used the same, out of transaction. That's not the point. It is better to create it anew inside the method.

G
galaxy, 2022-01-26
@galaxy

Maybe you can somehow figure it out with quotes after all? If you do not understand why they are needed, you should not put them anywhere and anyhow.

return self.cursor.execute("DELETE FROM queue WHERE chat_id = ?", (chat_id,))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question