为什么查询在PGAdmin中有效,但在Python代码中无效?

时间:2019-07-08 22:50:18

标签: python sql postgresql

我创建了一个SQL查询并在PGAdmin中对其进行了测试。初始查询返回了意外的结果。在()部分中使用WHERE的精确查询返回了我期望的结果。
在我的Python脚本中,优化查询返回一个空列表。

我需要遍历节点列表并运行查询。
()如何影响我的查询()导致空列表?

我尝试了将WHERE部分进行分组的各种方式,并且总是得到一个空列表。完全删除()的每条记录。

PGADMIN查询:

SELECT DISTINCT a.name CNA_Name, a.value CNA_Value
FROM nms_node n
LEFT OUTER JOIN nms_node_cna a
ON a.parent = n.id
WHERE   n.long_name  = '006-smtp-in-2l.ems365.company.com' and (a.name = 
'ENV' or a.name = 'Branch' or a.name = 'BRANCH' or a.name = 'ENV' or 
a.name = 'Program' or a.name = 'Support' or a.name = 'Tier' or a.name = 
'tier');

退货:

long_name   cna_name    cna_value
006-smtp-in-2l.ems365.company.com   Program TBD
006-smtp-in-2l.ems365.company.com   ENV Production
006-smtp-in-2l.ems365.company.com   BRANCH  Network_Services

PYTHON代码

OneLineQuery = """
SELECT DISTINCT  a.name CNA_Name , a.value CNA_Value 
FROM nms_node n 
LEFT OUTER JOIN nms_node_cna a 
ON a.parent = n.id 
WHERE  n.long_name  = '%s' and a.name in ('ENV', 'Branch', 'BRANCH', 
'ENV', 'Program', 'Support', 'Tier', 'tier')
""" %(''.join(i))


for i in JUST_NODES_LIST:
    cursor.execute(OneLineQuery.format(i))
    ii = cursor.fetchall()
    print (i,"\t",ii)


['006-smtp-in-2g.ems365.company.com']    []
['006-smtp-in-2i.ems365.company.com']    []
['006-smtp-in-2j.ems365.company.com']    []
['006-smtp-in-2k.ems365.company.com']    []
['006-smtp-in-2l.ems365.company.com']    []
['10.104.1.194']     []
['10.104.120.1']     []
['10.104.152.11']    []
['10.104.2.129']     []
['10.104.2.130']     []
['10.104.217.100']   []
['10.104.232.2']     []
['10.104.240.2']     []

我希望得到一个包含一个节点及其相关自定义属性的列表。 (Node, Attribute Name, Attribute Value)

JRFDTWPNFP001.csa.company.com,BRANCH,Data_Center_Services
JRFDTWPNFP001.csa.company.com,ENV,Production
JRFDTWPNFP001.csa.company.com,Program,TBD

然后我将合并到一条"JRFDTWPNFP001.csa.company.com,BRANCH,Data_Center_Services,ENV,Production,Program,TBD"

这样的记录中

0 个答案:

没有答案