Сделал код, который при запуске бота сохраняет user_id в БД SQLite.
При запуске бота терминал выдает ошибку
Task exception was never retrieved
future: <Task finished name='Task-9' coro=<Dispatcher._process_polling_updates() done, defined at C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\aiogram\dispatcher\dispatcher.py:407> exception=AttributeError('__enter__')>
Traceback (most recent call last):
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 415, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 235, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 256, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\артем\Desktop\bot\main1.py", line 41, in start
if not db.userexists(message.chat.id):
File "C:\Users\артем\Desktop\bot\db.py", line 9, in userexists
with self.connection:
AttributeError: __enter__
а вот код main.py
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
if message.chat.type == 'private':
if not db.userexists(message.chat.id):
db.add_user(message.chat.id)
await bot.send_message(message.chat_id, "q")
а это database.py
import sqlite3
class Database:
def __init__(self, db_dile):
self.connection = sqlite3.connect(db_dile)
self.connection = self.connection.cursor()
def userexists(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT * FROM `users` WHERE `user_id` = ?", (user_id,)).fetchmany(1)
return bool(len(result))
def add_user(self, user_id):
with self.connection:
return self.cursor.execute("INSERT INTO `users` (`user_id`) VALUES (?)", (user_id,))