Home>

I recently started coding telegram bots with a database in Python using the pytelegrambotAPI and sqlite3 libraries, and ran into a problem. The code does not output the value specified by the user. The code:

import sqlite3
token=
bot= telebot.TeleBot (token)
db= sqlite3.connect ('data.db', check_same_thread= False)
cursor= db.cursor ()
cursor.execute ('' 'CREATE TABLE IF NOT EXISTS tbl (
    user_id integer,
    status string
) '' ')
@ bot.message_handler (commands= ['start'])
def welcome (message):
    markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
    item1= types.KeyboardButton ("test func")
    markup.add (item1)
    bot.send_message (message.chat.id, "Hello {0.first_name}! \ nI am {1.first_name}!". format (message.from_user, bot.get_me ()), reply_markup= markup)
@ bot.message_handler (content_types= ['text'])
def main (message):
    if message.chat.type== 'private':
        if message.text== "Back":
            markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
            item1= types.KeyboardButton ("test func")
            markup.add (item1)
            bot.send_message (message.chat.id, "Back" .format (message.from_user, bot.get_me ()), reply_markup= markup)
        elif message.text== "test func":
            cursor.execute (f "SELECT` status` FROM `tbl` WHERE` user_id`= '{message.from_user.id}' ")
            if cursor.fetchone () is None:
                markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
                item1= types.KeyboardButton ("1a")
                item2= types.KeyboardButton ("2a")
                back= types.KeyboardButton ("back")
                markup.add (item1, item2, back)
                bot.send_message (message.chat.id, "test func" .format (message.from_user, bot.get_me ()), reply_markup= markup)
            elif cursor.fetchone ()== '1':
                markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
                back= types.KeyboardButton ("back")
                markup.add (back)
                bot.send_message (message.chat.id, "1" .format (message.from_user, bot.get_me ()), reply_markup= markup)
            elif cursor.fetchone ()== "2":
                markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
                back= types.KeyboardButton ("back")
                markup.add (back)
                bot.send_message (message.chat.id, "2" .format (message.from_user, bot.get_me ()), reply_markup= markup)
        elif message.text== "1a":
            cursor.execute ('INSERT INTO tbl VALUES (?,?)', (message.from_user.id, '1'))
            db.commit ()
            markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
            back= types.KeyboardButton ("back")
            markup.add (back)
            bot.send_message (message.chat.id, "1" .format (message.from_user, bot.get_me ()), reply_markup= markup)
        elif message.text== "2a":
            cursor.execute ('INSERT INTO tbl VALUES (?,?)', (message.from_user.id, '2'))
            db.commit ()
            markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
            back= types.KeyboardButton ("back")
            markup.add (back)
            bot.send_message (message.chat.id, "2" .format (message.from_user, bot.get_me ()), reply_markup= markup)
bot.polling (none_stop= True)

When executing test func for the first time, the code must write the data that the user enters, and when the function is executed the second time it must display the value that the user specified the first time he used it. The bot does not output. Thank you in advance.

Please clarify your specific problem or provide more detailed information on what exactly you need. As it stands, it is difficult to understand exactly what you are asking.

Дух сообщества2021-12-21 15:09:09
  • Answer # 1

    Functionfetchonereturns a tuple for existing data, so your check, likecursor.fetchone ()== '1'(for example, if the status'one', then the check would be('1',)== '1'), always will beFalse

    I would change the code to something like this:

    elif message.text== "test func":
                sql= "SELECT` status` FROM `tbl` WHERE` user_id`=? "
                result= cursor.execute (sql, [message.from_user.id]). fetchone ()
                status= result [0] if result else None
                if status is None:
                    markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
                    item1= types.KeyboardButton ("1a")
                    item2= types.KeyboardButton ("2a")
                    back= types.KeyboardButton ("back")
                    markup.add (item1, item2, back)
                    bot.send_message (message.chat.id, "test func" .format (message.from_user, bot.get_me ()), reply_markup= markup)
                elif status== '1':
                    markup= types.ReplyKeyboardMarkup (resize_keyboard= True)
                    back= types.KeyboardButton ("back")
                    markup.add (back)
                    bot.send_message (message.chat.id, "1" .format (message.from_user, bot.get_me ()), reply_markup= markup)
                elif status== "2":
                    ...