M
M
MIL_12342021-10-19 23:28:09
Python
MIL_1234, 2021-10-19 23:28:09

How can I prevent duplicate records from being entered into the database?

The user's city is entered into the database when he writes his city. If he writes the city again, then the bot crashes, because. there is already a city in the database. How to solve this problem?

Mistake:

sqlite3.IntegrityError: UNIQUE constraint failed: user_city

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yephim, 2021-10-19
@Yephim

Check for city before writing
or
handle IntegrityError with try except

K
Kadabrov, 2021-10-19
@Kadabrov

Your entire column is made unique, so you can't add a subsequent entry with that city

M
Max, 2021-10-20
@MaxDukov

depending on what you want to achieve. If the city really needs to be unique, check for the existence of such a city before recording. If more than 1 user can live in one city, remove uniqueness.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question