I am just learning SQL and apologize if this is repetitive, but I have searched and not found an answer. Writing in python with a PostgreSQL DB.
I want to check a table in my database to see if the "Port" column contains a specific location. I can do this interactively, but it returns a table result, eg:
select exists(select 1 from ref_locations where "Port"='ORAN');
it returns:
exists
-------
t
(1 row)
... I want it to return JUST a boolean value, and I want to do it in my python script. In my python script I have:
exists = False
cnx.execute("""select exists(select 1 from ref_locations where "Port"='BAKU');""")
print "Exists: ",exists
but exists is always filled with False. Any help greatly appreciated.
print "Exists: ",existsyour exists variable is not changed by the result of the query. You need to retrieve the result from the query, and assign it to your variable. There probably is some function in your cursor, likeexists = cnx.get_result();