skip to Main Content

I’m making a telegram bot in python. The fact is that information should be output from the users table, but only the ‘id’ label should be output. And when I translate row to a row, the data is output, but when changing/adding information, the bot outputs the same information (I work with mysql via worbench). Here bot.py:

import telebot
import config
import pymysql
import os 




from telebot import types

import pymysql.cursors

# Connect to the database
try:
    connection = pymysql.connect(host='localhost', user='root', password='password', database='ts', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
print("connected")

try:
    with connection:
        cur = connection.cursor()
        cur.execute("SELECT  * FROM users ORDER BY id DESC LIMIT 1")

        result = cur.fetchall()



        bot = telebot.TeleBot(config.token)



        @bot.message_handler(commands=['session'])
        def lalala(message):
                for row in result:
                     bot.send_message(message.chat.id, row)


        bot.polling(none_stop=True)


finally:
    connection.close()

except Exception as ex:
print("conn refused")

Thank you in advance!

2

Answers


  1. Chosen as BEST ANSWER

    It turned out that in each function it is necessary to reconnect to MySQL

    Code:

    from telebot import types
    import pymysql.cursors
    
    
    bot = telebot.TeleBot(config.token)
    
    
    
    
    
    @bot.message_handler(commands=['session'])
    def lalala(message):
        connection = pymysql.connect(host='localhost', user='root', password='password', database='ts', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
        cur = connection.cursor()
        cur.execute("SELECT  * FROM users ORDER BY id DESC LIMIT 1")
        result = cur.fetchone()
    
        bot.send_message(message.chat.id, f'{result}')
    
    
    bot.polling(none_stop=True)
    

  2. You can’t ave a try without an except or finally.

    so changing the code a bit you can connect to a local dataase

    import telebot
    import config
    import pymysql
    import os 
    
    
    
    
    from telebot import types
    
    import pymysql.cursors
    
    
    try:
        connection = pymysql.connect(host='localhost', user='root', password='passpord', database='ts', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
        print("connected")
    except:
        print("not connected")    
    
    
    try:
        with connection:
            cur = connection.cursor()
            cur.execute("SELECT  * FROM users ORDER BY id DESC LIMIT 1")
    
            result = cur.fetchall()
    
    
    
            bot = telebot.TeleBot(config.token)
    
    
    
            @bot.message_handler(commands=['session'])
            def lalala(message):
                    for row in result:
                         bot.send_message(message.chat.id, row)
    
    
            bot.polling(none_stop=True)
    
    
    finally:
        connection.close()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search