我正在学习SQL,如果这很重复,我很抱歉,但我已经搜索过了,没有找到答案。使用Python编写,使用PostgreSQL数据库。
我想检查我的数据库中的一个表格,看看“Port”列是否包含特定位置。我可以通过交互方式完成此操作,但它返回一个表结果,例如:
它返回:
我希望它只返回一个布尔值,并且我想在我的Python脚本中实现。 我的Python脚本中有:
但存在总是充满了虚假。非常感谢您的帮助。
我想检查我的数据库中的一个表格,看看“Port”列是否包含特定位置。我可以通过交互方式完成此操作,但它返回一个表结果,例如:
select exists(select 1 from ref_locations where "Port"='ORAN');
它返回:
exists
-------
t
(1 row)
我希望它只返回一个布尔值,并且我想在我的Python脚本中实现。 我的Python脚本中有:
exists = False
cnx.execute("""select exists(select 1 from ref_locations where "Port"='BAKU');""")
print "Exists: ",exists
但存在总是充满了虚假。非常感谢您的帮助。
print "Exists: ",exists
your 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();
- wildplasser