1

База данных:

import sqlite3

class SQLighter:

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

    def subscriber_exists(self, user_id):
        with self.connection:
            result = self.cursor.execute('SELECT * FROM `subscriptions` WHERE `user_id` = ?', (user_id,)).fetchall()
            return bool(len(result))
    
    def update_subscription(self, user_id, status):
        with self.connection:
            return self.cursor.execute("UPDATE `subscriptions` SET `status` = ? WHERE `user_id` = ?", (status, user_id))

Бот:

import telebot
from telebot import types
from sqlighter import SQLighter
db = SQLighter('db.db')

@bot.message_handler(commands=['subscribe'])
def subscribe(message: types.Message):
    if (not db.subscriber_exists(message.from_user.id)):
        db.add_subscriber(message.from_user.id)
    else:
        db.update_subscription(message.from_user.id, True)
    bot.send_message(message.chat.id,' Вы успешно подписались')

Сама ошибка:

sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 9700 and this is thread id 9344

1 ответ 1

1

Достаточно перевести ошибку на русский, если не знаете английского:

sqlite3.ProgrammingError: объекты SQLite, созданные в потоке, могут использоваться только в том же потоке. Объект был создан в потоке с идентификатором 9700, и это поток с идентификатором 9344.

Вот другой ответ, в котором посоветовали сделать так:

conn = sqlite3.connect('database.db', check_same_thread=False)

Впрочем нельзя чтобы к одной sqlite базе было два запроса ондовреммено, это ее покоцает. Be careful and good luck! ;)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.