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
It turned out that in each function it is necessary to reconnect to MySQL
Code:
You can’t ave a try without an except or finally.
so changing the code a bit you can connect to a local dataase