Home > Net >  Parsing the return of SQLite Query in Python
Parsing the return of SQLite Query in Python

Time:01-29

I have an SQLite database in a Flask (Python) web application and this is my code to see if an email has already registered (in my application.py file). It returns a boolean (1 or 0) if it exists or not, respectively.

db = SQL("sqlite:///test.db")

exists = db.execute("SELECT EXISTS(SELECT 1 FROM people WHERE email in (?))", ([email protected]))

Where print(exists) returns:

[{"EXISTS(SELECT 1 FROM people WHERE email in ('[email protected]'))": 1}]

In short, I get the required result (tested with unregistered and registered emails), but I don't know how to access this value of 1 or 0.

Would appreciate any ideas a lot.

CodePudding user response:

Try to alias the exists part of your select query

exists = db.execute("SELECT EXISTS(SELECT 1 FROM people WHERE email in (?)) as email_exists", ([email protected]))
        print(exists[0])

then you should be able to access the value by exists[0]['email_exists']. This will give you 1 or 0 which you can next convert to bool.

exists = db.execute("SELECT EXISTS(SELECT 1 FROM people WHERE email in (?)) as email_exists", ([email protected]))
        print(bool(exists[0]['email_exists']))
  •  Tags:  
  • Related