P
P
PuffmedPfone2020-07-02 19:57:03
MySQL
PuffmedPfone, 2020-07-02 19:57:03

How to check if certain data exists in MySQL DB in python?

I am writing my first bot for Telegram (TelebotAPi library (telebot)), and I need the bot to register user data in the database .. in principle, I managed to do it, but if I send "/start" to it again, it will again add data to the database . How to check for the presence of data in the database?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Guerro69, 2020-07-02
@PuffmedPfone

SELECT * FROM ваша_таблица WHERE ваш_параметр=чему-то.
# Например
SELECT * FROM MyTable WHERE user_id=12345678

This request must be made into a variable, and this request will return either 0 if more than one person was not found, or 1+, the number that returned and the number of records found with this id.

C
cython, 2020-07-02
@cython

You make a request to search for a user in the database (instead of $1 - user id): Next, make a check that returned None, otherwise add the user.
SELECT id FROM users WHERE id = $1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question