M
M
Misha Utka2021-05-20 19:18:49
Python
Misha Utka, 2021-05-20 19:18:49

How to make sure that the information in the database is not repeated sql telebot?

60a68b68c79ef354399304.png
In python, for each user, a tag is generated and written to the table, if a person wants, he can change it. How to make it so that when editing it, a person could not put a tag that another user already has.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2021-05-20
@ut1ka

You can impose a unique index on the field, then when you try to make a duplicate, there will be a sql error that can be intercepted and processed as you need, for example, ask to change the selected tag.
If sql error does not suit you, then you can try to check for the existence of the tag value in the database before inserting / changing the tag, for example:
select 1 from table where tag = 'New tag'
If the query returned 1, then this tag is already in the database and you need to come up with another one.

V
Vasily Bannikov, 2021-05-20
@vabka

In all SQL databases, you can add a unique index, which will ensure the uniqueness of the values ​​in the column.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question