Home > Software engineering >  How do I check if item exist in database?
How do I check if item exist in database?

Time:11-29

name_search = request.form.get("name_search") #I will get an input from a form
name = db.execute("SELECT name FROM birthdays") #get the list of names in db

how do i check if name_search exist in name?

CodePudding user response:

Check this:

if db.execute("SELECT 1 FROM birthdays WHERE name = ?", [name_search]).fetchone() is not None:
    pass
    # record exists
else:
    pass
    # record doesn't exist

CodePudding user response:

db.execute(f"SELECT name FROM birthdays WHERE name = {name_search}")

This should give you no result if there is no record..

  • Related