I'm trying to check whether a variable exists in an SQLite3 db. Unfortunately I can not seem to get it to work. The airports table contains 3 colums, with ICAO as the first column.
if c.execute("SELECT EXISTS(SELECT 1 FROM airports WHERE ICAO='EHAM')") is True:
print("Found!")
else:
print("Not found...")
The code runs without any errors, but the result is always the same (not found).
What is wrong with this code?
Try this instead:
Return value of
cursor.execute
is cursor (or to be more precise reference to itself) and is independent of query results. You can easily check that:From the other hand if you call
cursor.fetchone
result tuple or None if there is no row that passes query conditions. So in your caseif c.fetchone():
would mean one of the below:or