Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较Python中的两个查询结果_Python - Fatal编程技术网

比较Python中的两个查询结果

比较Python中的两个查询结果,python,Python,我是python新手,正在尝试编写一个脚本来比较来自两个数据库的数据 特别是,尝试生成输出以列出ecah客户端的匹配/取消匹配序列 有两个数据库 AORCL数据库 SQL> select * from ffduser.client_mapping; CLIENTNO CLIENTSEQ STATUS ---------- ---------- ---------- 1 1000 VALID 2 2000 VALID SQL

我是python新手,正在尝试编写一个脚本来比较来自两个数据库的数据

特别是,尝试生成输出以列出ecah客户端的匹配/取消匹配序列

有两个数据库

AORCL数据库

SQL> select * from ffduser.client_mapping;
  CLIENTNO  CLIENTSEQ STATUS
---------- ---------- ----------
         1       1000 VALID
         2       2000 VALID
SQL> select * from ffduser.client_mapping;
  CLIENTNO  CLIENTSEQ STATUS
---------- ---------- ----------
         1       1000 VALID
         2       2002 VALID
BORCL数据库

SQL> select * from ffduser.client_mapping;
  CLIENTNO  CLIENTSEQ STATUS
---------- ---------- ----------
         1       1000 VALID
         2       2000 VALID
SQL> select * from ffduser.client_mapping;
  CLIENTNO  CLIENTSEQ STATUS
---------- ---------- ----------
         1       1000 VALID
         2       2002 VALID
预期产量

Clientno 1 has Clientseq 1000 in AORCL and 1000 in BORCL -> Match
Clientno 2 has Clientseq 2000 in AORCL and 2001 in BORCL -> UnMatch
==========

我写了下面的代码,但无法生成预期的输出。 我认为它可能需要嵌套循环,但我是新手。 非常感谢您的帮助

$ cat test.py
#!/usr/bin/pytho
import cx_Oracle
pdb_name = 'AORCL'
ddb_name = 'BORCL'
pserver_name = 'ff1db03'
dserver_name = 'ff1db03'
pcon = cx_Oracle.connect('system/xxxxx@'+pserver_name+':1521/'+pdb_name)
dcon = cx_Oracle.connect('system/xxxxx@'+dserver_name+':1521/'+ddb_name)
pcur = pcon.cursor()
dcur = dcon.cursor()
pstat = pcur.execute('select clientno,clientseq from ffduser.client_mapping order by clientno').fetchall()

for plogresult in pstat:
    print (pdb_name +'->'+ ' Client# '+str(plogresult[0])+' Seq No# ' +str(plogresult[1]))

dstat = dcur.execute("select clientno,clientseq from ffduser.client_mapping where status='VALID' order by clientno").fetchall()
for dlogresult in dstat:
        print (pdb_name +'->'+' Client# '+str(dlogresult[0])+' Seq No# ' +str(dlogresult[1]))

pcur.close()
pcon.close()
dcur.close()
dcon.close()


$ python test.py
AORCL-> Client# 1 Seq No# 1000
AORCL-> Client# 2 Seq No# 2000
AORCL-> Client# 1 Seq No# 1000
AORCL-> Client# 2 Seq No# 2002

我想您需要一种方法,我会称之为select\u to\u name\u value\u mappingtable\u name

此方法返回一个字典,其中名称为键,Eno为值

mapping1=select_to_name_value_mapping('table1')
mapping2=select_to_name_value_mapping('table2')

for key1, value1 in mapping1.items():
    value2=mapping2.get(key1)
    print('key=%s table1=%s table2=%s' % (key1, value1, value2))

print('missing in mapping1: %s' % set(mapping1.keys())-set(mapping2.keys()))
select_to_name_value_mappingtable_name的实现取决于您:-


上面是Python的实现。人脑更容易找到这种迭代方法。您可以使用SQL获得相同的结果,但更难获得集合中的思想解决方案。我的建议:学习成套思维。

欢迎来到StackOverflow。坐一会儿。阅读创建一个。为什么不对名称进行联接呢?您的方法有两个问题:如果一个键在mapping1中,但不在mapping2中,则输出None并将其作为缺少项包含在mapping1条目中。如果一个键在mapping2中,但不在mapping1中,它将永远不会被打印出来。@CodingLambdas我知道我的解决方案并不完美。这是一个如何解决这个问题的提示。