I am trying to check if a row exist with the same Name my database with python and can\'t quite get it here is what I am trying: (I know the connection is wokring)
If you want to check for empty results, try if cur.description is None
:
if cursor.description is None:
# No recordset for INSERT, UPDATE, CREATE, etc
pass
else:
# Recordset for SELECT
As well as:
exist = cursor.fetchone()
if exist is None:
... # does not exist
else:
... # exists
If you are running a statement that would never return a result set (such as
INSERT
withoutRETURNING
, orSELECT ... INTO
), then you do not need to call.fetchall()
; there won't be a result set for such statements. Calling.execute()
is enough to run the statement.