Ошибка SQLite objects created in a thread can only be used in that same thread. The object was created in thread id. Как решить?

В программировании супер новичок, но необходимо за короткие сроки создать бота с базой данных для аудиоэкскурсии. Нужно, чтобы он не добавлял в БД, а проверял полученный от юзера номер телефона в БД и после давал или не давал доступ к боту.

Выдает две постоянные ошибки:

  1. File "C:\Users\Валерия\Downloads\pyTelegramBotAPI-4.6.0\pyTelegramBotAPI-4.6.0\run.py", line 21, in check_contact if(not Database.user_exists(message.from_user.id)): TypeError: Database.user_exists() missing 1 required positional argument: 'contact' File "C:\Users\Валерия\Downloads\pyTelegramBotAPI-4.6.0\pyTelegramBotAPI-4.6.0\db.py", line 9, in user_exists with self.connection:
  2. sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 7288 and this is thread id 9788.

Бот:

import telebot
from telebot import types
from db import Database
bot = telebot.TeleBot('токен')
db='название бд'
@bot.message_handler(commands=['start'])
def start(message):
    kb = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=1, one_time_keyboard=True)
    btn1=types.KeyboardButton(text="Поделиться телефоном", request_contact=True)
    kb.add(btn1)
    bot.send_message(message.chat.id, text="Для активации необходимо нажать на кнопку и поделиться своим номером телефона. Так я пойму, что у тебя есть доступ к экскурсии, удачи!", reply_markup=kb)
@bot.message_handler(content_types=['contact'])
def check_contact(message):
    if message.contact.user_id == message.from_user.id:
       a = telebot.types.ReplyKeyboardRemove()
    bot.send_message(message.from_user.id, 'Проверяю!', reply_markup=a)
      if(not Database.user_exists(message.from_user.id)):
      bot.send_message (message.from_user.id, 'К сожалению, Вас нет в моей базе данный:(')
      else:
      bot.send_message (message.from_user.id, 'Поздравляю! Ты есть в моей базе')
      markup = types.InlineKeyboardMarkup(row_width=1) 
      item = types.InlineKeyboardButton('Начало маршрута', callback_data=1)
      markup.add(item)
      bot.send_message(message.chat.id, 'Готов? Жми кнопку Начало маршрута', reply_markup=markup)
    else:
    bot.send_message(message.chat.id, 'Это не ваш номер телефона')
@bot.callback_query_handler(func=lambda call: True)
def callback (call):
    if call.message:
        if call.data == '1':
         bot.send_photo (call.message.chat.id, photo=open('карта1.jpg', 'rb'))
         markup = types.InlineKeyboardMarkup(row_width=1)
         item2 = types.InlineKeyboardButton('На месте', callback_data=2)
         markup.add(item2)
         bot.send_message (call.message.chat.id, 'На месте?', reply_markup=markup)
        if call.data == '2':
        bot.send_audio (call.message.chat.id, audio=open('09.After Dark.mp3', 'rb'))
bot.polling(non_stop=True)

БД:

import sqlite3
class Database:
    def __init__(self, db_file):
        self.connection = sqlite3.connect(db_file)
        self.cursor = self.connection.cursor()
    def user_exists(self, contact):
        with self.connection:
        result=self.cursor.execute("SELECT 1 * FROM `guys` WHERE `contact` = ?", (contact,)).fetchall()   
        return bool(len(result))
    def close(self):
        self.connection.close()

БД в SQLiteStudioвведите сюда описание изображения

Сам бот отлично работал до того, как я начала подключать БД, путаюсь, что куда вставлять, но бот мне очень нужен. Помогите, пожалуйста, умные люди.


Ответы (0 шт):