lundi 4 mai 2015

Check if a row exists in sqlite3?

How do I properly check if a row exists in my database table using Python 3 and sqlite3?

Here's my original code:

cursor.execute("SELECT name, age FROM people WHERE pin=?", (pin, ))
name, age = cursor.fetchone() or ('', 0)

So currently I'm setting them a default value with the or ('', 0), and I really don't like this.

Is there a way to check if the person already exists? Something like this (pseudo):

if cursor.exists("pin IN people"):
    cursor.execute("SELECT name, age FROM people WHERE pin=?", (pin, ))
    name, age = cursor.fetchone()

Aucun commentaire:

Enregistrer un commentaire