大家下午好。
我有一個電報機器人,我希望機器人注冊用戶并將他們的答案輸入資料庫。但問題是它注冊了用戶 ID,但沒有使用其他用戶回應(姓名、電子郵件、電話)更新資料庫中的記錄。
順便說一下,我使用 aiogram 庫來創建電報機器人。
這是主要的鱈魚:
class FSMRegistration (StatesGroup):
cl_name= State()
cl_phone=State()
cl_email=State()
db = THEPARALLEL_DB('35thparallelClientBase.db')
@dp.callback_query_handler(text='reg')
@dp.callback_query_handler(text='cansel_1')
async def registration(query: types.CallbackQuery):
answ_data = query.data
if answ_data == 'reg':
if (not db.client_exists(query.from_user.id)):
await bot.send_message(query.from_user.id, text=f'Lest do it! Write: next')
@dp.message_handler(Text(equals='next', ignore_case=True), state=None)
async def reg_start(message: types.Message):
await FSMRegistration.cl_name.set()
await message.reply('Write your First and Last name')
if (len(message.text) > 20):
await message.reply(
('The first and last name must not exceed more than 20 characters'))
elif '@' in message.text or '/' in message.text or '?' in message.text or '#' in message.text or '$' in message.text or '%' in message.text:
await message.reply(message.from_user.id, "You have entered a forbidden symbol.")
else:
db.add_client(message.from_user.id)
pass
@dp.message_handler(state="*", commands='cansel')
@dp.message_handler(Text(equals='отмена', ignore_case=True), state="*")
async def cansel_handler(message: types.Message, state: FSMContext, current_state=None):
current_state * await state.get_state()
if current_state is None:
return
await state.finish()
await message.reply('Ok!')
@dp.message_handler(state=FSMRegistration.cl_name)
async def set_cl_name(message: types.Message, state:FSMContext):
async with state.proxy() as data:
db.set_client_name(message.from_user.id, message.text)
data['cl_name']=message.text
await FSMRegistration.next()
await message.reply("Great! Please write your phone:")
@dp.message_handler(state=FSMRegistration.cl_phone)
async def set_cl_phone(message: types.Message, state: FSMContext):
async with state.proxy() as data:
if (len(message.text) > 12):
await message.reply(('There are too many symbols in your message'))
elif int(message.text):
data['cl_phone'] = message.text
db.set_client_phone(message.from_user.id, message.text)
await FSMRegistration.next()
await message.reply("Great, one more step behind! Your email:")
@dp.message_handler(state=FSMRegistration.cl_email)
async def set_cl_email(message: types.Message, state: FSMContext):
async with state.proxy() as data:
if '@' not in message.text and '.' not in message.text:
await message.reply(
('Error! You have lost this symbol (@)'))
else:
data['cl_email'] = message.text
db.set_client_email(message.from_user.id, message.text)
await message.reply("Succses, Thank You!")
await state.finish()
我使用 sqllite3 作為資料庫:
class THEPARALLEL_DB():
def __init__(self, database_file):
self.connection = sqlite3.connect(database_file)
self.cursor=self.connection.cursor()
def client_exists(self, user_id):
with self.connection:
result = self.cursor.execute("SELECT * FROM 'clients' WHERE 'user_id' = ?", (user_id,)).fetchall()
return bool(len(result))
def get_user_id (self, user_id):
result = self.cursor.execute("SELECT 'id' FROM 'clients' WHERE 'user_id'= ?", (user_id,))
return result.fetchall()[0]
def add_client(self, user_id):
with self.connection:
return self.cursor.execute("INSERT INTO 'clients' ('user_id') VALUES (?)", (user_id,))
def set_client_name(self, user_id, client_name):
with self.connection:
return self.cursor.execute("UPDATE 'clients' SET 'client_name' = ? WHERE 'user_id' = ?",
(client_name, user_id,))
def set_client_phone(self, user_id, cl_phone):
with self.connection:
return self.cursor.execute("UPDATE 'clients' SET 'Phone' = ? WHERE 'user_id' = ?", (user_id, cl_phone,))
def set_client_email(self, user_id, cl_email):
with self.connection:
return self.cursor.execute("UPDATE 'clients' SET 'Email' = ? WHERE 'user_id' = ?", (user_id, cl_email,))
uj5u.com熱心網友回復:
您在背景關系管理器中使用了錯誤的游標。正確的應該是:
with self.connection:
self.connection.execute("INSERT INTO 'clients' ('user_id') VALUES (?)", (user_id,))
您還可以在查詢上打開一個新游標:
with contextlib.closing(self.connection.cursor()) as cursor:
cursor.execute(...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/337758.html
