I
I
Ilkhomjon Matazimov2020-05-12 13:43:12
MySQL
Ilkhomjon Matazimov, 2020-05-12 13:43:12

How to compare values ​​from a database?

Good day.
There is a database that stores a person's data. I compare by id, but it doesn't work for me.

Here is the code:

cursor = db.cursor()
cursor.execute("SELECT telegram_user_id FROM users")
result = cursor.fetchall()
if message.from_user.id not in result: # вот так не работает, работает только если писать result[0] и т.д.
    print("Выдает не зареган... Даже, если этот id есть в бд")
else:
    print("Вы уже зареганы в бд")


As I described in the comments, what does not work if you write just result.
And with indexes like "[0]" and so on. I cannot write, because there can be an infinite number of users.
How to solve it?

Here is the DB itself:
5eba7db57ed0c602019326.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bubaley, 2020-05-12
@mr_qpdb

In result you get an array of objects with one field. First you need to get the id array. This is to check for entry.
In your case, it's better to add where telegram_user_id = message.from.user_id to the query.
And check if the number of results in the sample is greater than or equal to 1, then it is registered.

Z
zexer, 2020-05-12
@zexer

lists = [(929992640,), (929992640,), (929992640,)]
lists = [i[0] for i in lists]
929992640 in lists # True

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question