S
S
Slanty2019-07-25 17:25:39
Python
Slanty, 2019-07-25 17:25:39

How to check if a record exists in a database (SQLite)?

I have a sqlite database, it has the following columns: id, name
How can I make sure that the same user cannot log into the database a second time?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Yakushenko, 2019-07-25
@Slanty

cursor = db.execute('SELECT * FROM table_name WHERE (id IS ? AND name IS ?)', (id, name))
row = cursor.fetchone()
if row is None:
    cursor = db.execute('INSERT INTO table_name (id, name) VALUES (?,?)', (id, name))
    db.commit()

S
Sergey Tikhonov, 2019-07-25
@tumbler

Make id a unique key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question