Ошибка в боте Телеграмм
Такая проблема. Появляется после запуска бота.
Libraries
python 3.9
aiogram 3.1.1
PyMySQL 1.1.o
asyncio 3.4.3
MySQL 5.5.23
PMA 3.5.1
Cause exception while process update id=742053270 by bot id=6871058034
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'user_id' = 1628358150' at line 1")
Traceback (most recent call last):
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\dispatcher.py", line 308, in _process_update
response = await self.feed_update(bot, update, **kwargs)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\dispatcher.py", line 157, in feed_update
response = await self.update.wrap_outer_middleware(
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\middlewares\error.py", line 25, in __call__
return await handler(event, data)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\middlewares\user_context.py", line 27, in __call__
return await handler(event, data)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\fsm\middleware.py", line 41, in __call__
return await handler(event, data)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\event\telegram.py", line 120, in trigger
return await wrapped_inner(event, kwargs)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\event\handler.py", line 42, in call
return await wrapped()
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\dispatcher.py", line 275, in _listen_update
return await self.propagate_event(update_type=update_type, event=event, **kwargs)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\router.py", line 116, in propagate_event
return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\router.py", line 111, in _wrapped
return await self._propagate_event(
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\router.py", line 136, in _propagate_event
response = await observer.trigger(event, **kwargs)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\event\telegram.py", line 120, in trigger
return await wrapped_inner(event, kwargs)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\aiogram\dispatcher\event\handler.py", line 42, in call
return await wrapped()
File "c:\Users\lipuha\Desktop\TG_Bot\main.py", line 19, in start
if(not db.user_exists(message.from_user.id)):
File "c:\Users\lipuha\Desktop\TG_Bot\db.py", line 22, in user_exists
result = self.cur.execute("SELECT * FROM 'users' WHERE 'user_id' = %s", user_id).fetchall()
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\cursors.py", line 153, in execute
result = self._query(query)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\cursors.py", line 322, in _query
conn.query(q)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\connections.py", line 558, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\connections.py", line 822, in _read_query_result
result.read()
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\connections.py", line 1200, in read
first_packet = self.connection._read_packet()
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\connections.py", line 772, in _read_packet
packet.raise_for_error()
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "C:\Users\lipuha\AppData\LPWKtcPjpxQzc26CxTtPFuQMNfm2RfEHfK\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users' WHERE 'user_id' = 1628358150' at line 1")
main.py
import asyncio
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from aiogram.filters import Command, CommandObject, CommandStart
from db import Database
from config import TOKEN_TELEGRAM
import markups as kb
bot = Bot(TOKEN_TELEGRAM, parse_mode="HTML")
dp = Dispatcher()
db = Database()
@dp.message(CommandStart())
async def start(message: Message):
if(not db.user_exists(message.from_user.id)):
db.add_user(message.from_user.id)
await message.answer("Введите предпочитаемое имя: ")
else:
await message.answer("Приветуствую", reply_markup=kb.mainMenu)
@dp.message()
async def bot_message(message: Message):
if (db.get_signup(message.from_user.id)=='set_nickname'):
if "@" in message or '/' in message:
await message.answer('В имени не должный присутсвтовать следующие знаки: @, /')
elif len(message) > 15:
await message.answer('Имя не должно превышать 15 символов!')
else:
db.set_nickname(message.from_user.id, message)
db.set_signup(message.from_user.id, "done")
await message.answer("Регистрация прошла успешно!", reply_markup=kb.mainMenu)
else:
await message.answer('Не пон(')
async def main():
await bot.delete_webhook(drop_pending_updates=True)
await dp.start_polling(bot)
if __name__ == '__main__':
asyncio.run(main())
db.py
import pymysql
import pymysql.cursors
class Database:
def __init__(self):
self.con = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='',
database='tgbot',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
self.cur = self.con.cursor()
def add_user(self, user_id):
with self.con:
self.cur.execute("INSERT INTO 'users' ('user_id') VALUE (%s)", user_id)
def user_exists(self, user_id):
with self.con:
result = self.cur.execute("SELECT * FROM 'users' WHERE 'user_id' = %s", user_id).fetchall()
return bool(len(result))
def set_nickname(self, user_id, nickname):
with self.con:
return self.cur.execute("UPDATE 'users' SET 'nickname' = %s WHERE 'user_id' = %s", nickname, user_id)
def get_signup(self, user_id):
with self.con:
result = self.cur.execute("SELECT 'signup' FROM 'users' WHERE 'user_id' = %s", user_id,).fetchall()
for row in result:
signup = str(row[0])
return signup
def set_signup(self, user_id, signup):
with self.con:
return self.cur.execute("UPDATE 'users' SET 'signup' = %s WHERE 'user_id' = %s", signup, user_id)