T
T
Tayrus02019-12-28 16:45:55
Python
Tayrus0, 2019-12-28 16:45:55

How to protect a table from creating duplicates?

With this function I enter data into a table

def init_user_db(con, users):
    cursorObj = con.cursor()
    
    for x in users:
        limits_values = (x, 0)
        cursorObj.execute('INSERT INTO limits(id, count) VALUES(?, ?) ON CONFLICT DO NOTHING', limits_values)
    
    con.commit()

Through this function I look up the values
def sql_counts_checker(con, message):
    cursorObj = con.cursor()    
    cursorObj.execute(f'SELECT count FROM limits WHERE id = {message.chat.id}')
    
    rows = cursorObj.fetchall()
    print(rows)
    count = rows[0][0]

    con.commit()

    return count

When I execute the function init_user_dbseveral times, then in the output ( print(rows)) I will get duplicates , [(2,), (0,), (0,), (0,), (0,), (0,), (0,)]although I indicated ON CONFLICT DO NOTHINGwhy duplicates are created anyway? How to fix it (with code examples please)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2019-12-28
@Tayrus0

so there is no conflict)) you did not specify unique

A
Andrey, 2019-12-28
@anerev

https://www.sqlite.org/lang_UPSERT.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question