How to use a Postgreql Boolean value as a parameter in a python if statement?
Question:
I have been trying to create a python function that will use a select statement that will return a single Boolean value from my postgres database and then use it in a python if statement.
I have found out that when you print the result of the sql select statemtent the format is [(True,)]
. However, nothing that I have tried putting in the python if statement has not worked.
I have tried:
True
"True"
"[(True,)]"
"[(" + str(True) + ",)]"
and none of these have worked and I can’t really think of what else it could be.
The sql statement I am executing is
cur = conn.cursor()
cur.execute("SELECT is_draft FROM item_pool WHERE item_id = " + str(item_id) + " ", {})
list_of_tuples = cur.fetchall()
conn.close()
return list_of_tuples
And I want to check if the is_draft Boolean that is returned is True in python.
Answers:
In python
your function would return
return cursor.fetchone()[0]
which would return TRUE
or FALSE
for a query like
SELECT EXISTS(SELECT 1 FROMmytable WHERE id = -1)
I have been trying to create a python function that will use a select statement that will return a single Boolean value from my postgres database and then use it in a python if statement.
I have found out that when you print the result of the sql select statemtent the format is [(True,)]
. However, nothing that I have tried putting in the python if statement has not worked.
I have tried:
True
"True"
"[(True,)]"
"[(" + str(True) + ",)]"
and none of these have worked and I can’t really think of what else it could be.
The sql statement I am executing is
cur = conn.cursor()
cur.execute("SELECT is_draft FROM item_pool WHERE item_id = " + str(item_id) + " ", {})
list_of_tuples = cur.fetchall()
conn.close()
return list_of_tuples
And I want to check if the is_draft Boolean that is returned is True in python.
In python
your function would return
return cursor.fetchone()[0]
which would return TRUE
or FALSE
for a query like
SELECT EXISTS(SELECT 1 FROMmytable WHERE id = -1)