Отправляю запросы к базе sqlite возникает вот такая ошибка
Traceback (most recent call last):
File "C:/Users/Arseniy/PycharmProjects/RouletteBot/bot.py", line 24, in
bot.polling(none_stop=True)
File "C:\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\__init__.py", line 244, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "C:\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\__init__.py", line 268, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\util.py", line 103, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "C:\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\six.py", line 686, in reraise
raise value
File "C:\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\util.py", line 54, in run
task(*args, **kwargs)
File "C:/Users/Arseniy/PycharmProjects/RouletteBot/bot.py", line 21, in send_welcome
user_register(message.from_user.id)
File "C:/Users/Arseniy/PycharmProjects/RouletteBot/bot.py", line 14, in user_register
result = db.query('SELECT * FROM users WHERE tele_id=?', [(user_id)])
File "C:\Users\Arseniy\PycharmProjects\RouletteBot\db.py", line 9, in query
self.cur.execute(sql, args)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 6620 and this is thread id 9420
Код в котором отправляю запрос
import config
import msg
import sqlite3
from db import db
import telebot
db = db('users.db')
bot = telebot.TeleBot(config.TOKEN)
def user_register(user_id):
user_id = str(user_id)
result = db.query('SELECT * FROM users WHERE tele_id=?', [(user_id)])
if len(result.fetchall()) == 0:
db.query('INSERT INTO users (tele_id) VALUES (?)', [(user_id)])
@bot.message_handler(commands=['start'])
def send_welcome(message):
user_register(message.from_user.id)
bot.send_message(message.chat.id, msg.MSG_WELCOME)
bot.polling(none_stop=True)
Код класса с подключением
import sqlite3
class db:
def __init__(self, db):
self.con = sqlite3.connect(db)
self.cur = self.con.cursor()
def query(self, sql, args):
self.cur.execute(sql, args)
self.con.commit()
return self.cur
def __del__(self):
self.con.close()