A
A
Airat2021-08-05 16:06:48
Python
Airat, 2021-08-05 16:06:48

How to send order data one by one on "Next" command from Sqlite3 DB?

Hey! I am making a bot like fellow travelers on Aiogram and using the Sqlite3 database. I’m stuck on the fact that when I need to look for orders, I can’t display the order data in turn after the “Next” command, here is a loop and it displays everything, well, as if I already understood why, but I tried some more methods, it still didn’t work, how can be done to display the next line from the database with order data?

Here is the order search function code:

@dp.message_handler(commands="search_order", state=None)
async def cmd_search_order(message: types.Message):
    # Проверка, зарегестрирован ли пользователь, есть ли он в БД
    query_passenger_table = f'SELECT * FROM passengers WHERE id_passenger={message.from_user.id}'
    query_drivers_table = f'SELECT * FROM drivers WHERE id_driver={message.from_user.id}'

    check, check_passenger, check_driver = checking(query_passenger_table, query_drivers_table)
    if check == 1:
        # Проверка типа пользователя 
        query_passenger_table = f'SELECT * FROM passengers WHERE id_passenger={message.from_user.id}'
        query_drivers_table = f'SELECT * FROM drivers WHERE id_driver={message.from_user.id}'

        check, check_passenger, check_driver = checking(query_passenger_table, query_drivers_table)
        if check_passenger  == 1: 
            await message.answer('Выполняется поиск... Нажмите на кнопку "Дальше"', reply_markup=markup)
            
            @dp.message_handler(Text(equals="Забронировать"))
            async def with_puree(message: types.Message):
                # Сделать проверку на уже забронироавнные 2 места (добавить колонки в таблицу с заказом)
                await message.reply("Место забронировано! Уведомление отправлено водителю. \
                                    Он может с вами \связаться в личных сообщениях, вы тоже \
                                        можете с ним связаться, в заказе есть его данные для связи")


            @dp.message_handler(lambda message: message.text == "Дальше")
            async def without_puree(message: types.Message):
                
                connection = create_connection(PATH_DB)
                cursor = connection.cursor()

                query = "SELECT from_where, where_to, date, time, price, free_places, car_and_id, username FROM driver_orders"
                cursor.execute(query)

                while message.text == "Дальше":
                    next_row = cursor.fetchone()
                    if next_row:
                        await bot.send_message(message.chat.id,
                        md.text(
                            md.text('Связь: ', '@'+ str(next_row[7])),
                            md.text('Откуда: ', next_row[0]),
                            md.text('Куда: ', next_row[1]),
                            md.text('Дата: ', next_row[2]),
                            md.text('Время: ', next_row[3]),
                            md.text('Цена: ', next_row[4]),
                            md.text('Количество свободных мест: ', next_row[5]),
                            md.text('Машина: ', next_row[6]),
                            sep='\n',
                        ),
                        reply_markup=markup
                        )
                    else:
                        await message.answer("Заказы закончились. Начать поиск заново?")
                        break
elif check_driver == 1:
            await message.answer("Водители - /create_order") 
    else:
        await message.answer("Вы не зарегестрированы! Для регистрации введите /reg (или кликнуть)")


Here is the structure of the database
610be21a898e8089092251.jpeg

Another option is to leave this method, but under each add an Inline-button 'Book', supposedly the user himself will choose from the amount that the bot will send. After he has found a suitable order, he clicks Book and the whole place is booked, but it seems to me that there will be little performance if a large number of records are sent.

Here is how the code I have attached works
610be1d42567f546753898.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-05
@Ayrat-Developer

The answer is obvious. You need to store for the user at what position he stopped, and continue from there.
An easy way is to use the OFFSET and LIMIT modifiers in the SQL query. Then it will be sufficient to store just the number that will be used in OFFSET. At the initial request, set this number to 0, and by pressing further, increase it by the LIMIT value.
Technically, this can be done in a database table with "user id - number" pairs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question