Answer the question
In order to leave comments, you need to log in
How to get data from the database and display the bot in the telegram?
I welcome everyone. I am writing a bot in Python with the PyTelegramBotApi library. I can't figure out how to retrieve the data from the database. There is a completed base. That is, the user clicks on the buttons and for each button he sees the text that the bot receives from the database. Strictly do not judge this kind of question, I am self-taught, just learning. Thanks everyone)
Answer the question
In order to leave comments, you need to log in
It is necessary to make a request to the database itself.
Depending on the DBMS of the database, various libraries will help you with this.
for example, psycopg2 is suitable for Postgres .
The documentation provides examples of using the module.
import psycopg2
# Создание подключения к базе
conn = psycopg2.connect("dbname=test user=postgres") # Можно использовать формат DSN
# Создание курсора для выполнения операций с бд
cur = conn.cursor()
# Выполняешь запрос к бд
cur.execute("SELECT * FROM my_bd;")
cur.fetchone()
# выведет например (1, 100, "abc'def"), можно присвоить переменной
# После использования необходимо закрыть подключение и курсор.
cur.close()
сonn.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question