大家好!我决定在 aiogram3 和 postgresql 上创建一个项目,但是却走进了死胡同。该机器人的任务是接受关键词并使用它们在数据库中搜索信息。也就是说,如果我写/search 80那么它应该通过这个参数给出两个玩家的值(数据库在下面,在这种情况下是lennet和pico),但它只给出1个人的信息。我考虑尝试通过 for 循环来完成此操作,但没有成功。这是基础:
代码如下:
@dp.message(Command('search'))
async def search(message: Message):
#тут будет поиск в бд
arg = message.text.split() #получаем данные юзера
args = str(arg.pop(1)) #удаляем элемент т.к arg содержит в себе /search
try:
connection = psycopg2.connect( # конектимся к бд
host="127.0.0.1",
user="postgres",
password="staff",
database="wow_klass№2",
port="5432"
)
connection.autocommit = True
zapros = f"SELECT * FROM spek WHERE nikname = '{args}' OR class = '{args}' OR spek = '{args}' OR profa = '{args}' OR lvl = '{args}';"
int = f" SELECT COUNT(*) FROM spek WHERE nikname = '{args}' OR class = '{args}' OR spek = '{args}' OR profa = '{args}' OR lvl = '{args}';"
##### запрос в бд
with connection.cursor() as iner:
iner.execute(int)
integer = iner.fetchone()
with connection.cursor() as cursor:
cursor.execute(zapros)
#закончу тут, появился норм запрос и ответ, осталось реализовать больше 1 участника
tuple = cursor.fetchone()
nic = str(tuple[0])
clas = str(tuple[1])
spek = str(tuple[2])
prof = str(tuple[3])
lvl = str(tuple[4])
await message.answer(f"Ник игрока: {nic}. \nКласс: {clas}. \nСпек: {spek}. \nПрофесия: {prof}. \nlvl: {lvl}")
except Exception as _ex:
print(_ex)
finally:
if connection:
connection.close()
print("Отсоеденился успешно")