Y
Y
Yuraovdnk2020-04-13 13:57:57
Python
Yuraovdnk, 2020-04-13 13:57:57

Data output from the database to the telegram bot?

Hello. Need help. Here I wrote a simple bot that asks for data. Now we need to compare this data entered by the user with the data in the sqlite database (display if any). As I understand it, this data must be placed in a variable and in the "Select" query, compare the variable with the fields of the database. But I can't (lack of knowledge) Sorry if this is a dumb question, I'm new. Tell me please. Thanks

def city_start(message):
    try:
        markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
        itembtn1 = types.KeyboardButton('Киев')
        itembtn2 = types.KeyboardButton('Одесса')
        itembtn3 = types.KeyboardButton('Харьков')
        itembtn4 = types.KeyboardButton('Днепр')
        itembtn5 = types.KeyboardButton('Запорожье')
        itembtn6 = types.KeyboardButton('Львов')
        markup.add(itembtn1, itembtn2, itembtn3, itembtn4, itembtn5, itembtn6)
        msg = bot.send_message(chat_id, "Откуда отправляетесь?", reply_markup=markup)
        bot.register_next_step_handler(msg, city_finish)
    except Exception as e:
        bot.reply_to(message, 'oooops')

def city_finish(message):
    try:
        markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
        itembtn1 = types.KeyboardButton('Киев')
        itembtn2 = types.KeyboardButton('Одесса')
        itembtn3 = types.KeyboardButton('Харьков')
        itembtn4 = types.KeyboardButton('Днепр')
        itembtn5 = types.KeyboardButton('Запорожье')
        itembtn6 = types.KeyboardButton('Львов')
        markup.add(itembtn1, itembtn2, itembtn3, itembtn4, itembtn5, itembtn6)
        msg = bot.send_message(user_id, "Куда Направляетесь?", reply_markup=markup)
        bot.register_next_step_handler(msg, count_passanger)
    except Exception as e:
        bot.reply_to(message, 'oooops')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-04-13
@Yuraovdnk

and what is difficult to substitute a variable in the request?
get a city or whatever

city = message.text
    with db.connect('your.db') as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM table WHERE row = ?", (city,))
        result = cursor.fetchall()

if you modify it a bit, it could be something like this:
def check_city(city ):
    with db.connect('your.db') as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM table WHERE row = ? ", (city,))
        data = cursor.fetchone()
        if data is None:
            return False
        else:
            return data

test = check_city(message.text)
if test:
    bot.send_message(user_id, test[0])  # например первое поле из таблицы

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question