T
T
Tayrus02019-12-27 17:51:40
Python
Tayrus0, 2019-12-27 17:51:40

How to check for the existence of rows in a table in sqlite?

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(?, ?)', limits_values)
    
    con.commit()

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

How can I make sure that if the id and count rows already exist in the limits table, then they are not created?
That is, when I execute the function init_user_db1 time, I get 1 value [(2,)], but if I call this function a few more times, then the output will already be [(2,), (0,), (0,), (0,), (0,), (0,), (0,)], despite the fact that there is already there [(2,)], I need if there is a value, it is nothing more there did not bring

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2019-12-27
@Tayrus0

UPSERT , this is when an insert attempt is made, and if such an entry already exists, then it is either updated or the old value is stored.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question