In Python psycopg2 how can I check if a row exists?
def track_exists(self, track_id):
cur = self.conn.cursor()
cur.execute(\"SELECT fma_track_id FRO
Don't use fetchall() (which returns a list, which is never 'larger than 0'), use fetchone():
def track_exists(self, track_id):
cur = self.conn.cursor()
cur.execute("SELECT fma_track_id FROM tracks WHERE fma_track_id = %s", (track_id,))
return cur.fetchone() is not None
fetchone() returns None if there is nothing to fetch, and testing against is not None gives you a handy boolean value to return directly.