So I have a simple table of towns with Finnish and Swedish names:
CREATE TABLE town (id SERIAL PRIMARY KEY, name_fi TEXT, name_sv TEXT);
And I need to check if a particular town exists. It's rather easy:
cursor.execute(
"SELECT EXISTS ( SELECT 1 FROM town WHERE name_fi=%s OR name_sv=%s )",
(town, town)
)
result = cursor.fetchone() # True/False
But now I need to do this for multiple towns to make sure they all exist:
for town in towns:
# Code for one town from above
if result is False:
return False
return True
Is there a way to do this with one query instead of using a Python for
loop?