下面提到的代码用于比较相同或不同数据库中的两个表。我无法检索所需的结果以获得不匹配的记录。
我的问题:我无法打印表1&表2,因为我发现逐行迭代很困难。目前,即使是匹配的记录也被打印为不匹配的记录。
import psycopg2
conn_string = "host='localhost' dbname='dvdrental' user='postgres' password='jai'"
db1 = psycopg2.connect(conn_string)
db2= psycopg2.connect(conn_string)
cursor1=db1.cursor()
cursor2=db2.cursor()
cursor1.execute("select * from public.actor order by 1")
results1 = cursor1.fetchall()
cursor2.execute("select * from public.actor order by 1")
results2 = cursor2.fetchall()
count1 = len(results1)
count2 = len(results2)
# print count1
# print count2
# print results1
# print results2
# print results1[0]
# print results2[0]
for i in range(0,count1):
for j in range(0,count2):
if (results1[i] == results2[j]):
print "found"
else:
print "not found",results1
我尝试过下面的代码。请让我知道你的反馈
import psycopg2
conn_string = "host='localhost' dbname='dvdrental' user='postgres' password='jai'"
db1 = psycopg2.connect(conn_string)
db2= psycopg2.connect(conn_string)
cursor1=db1.cursor()
cursor2=db2.cursor()
cursor1.execute("select * from public.actor except select * from public.actor_1")
results1 = cursor1.fetchall()
cursor2.execute("select * from public.actor_1 except select * from public.actor")
results2 = cursor2.fetchall()
count1 = len(results1)
count2 = len(results2)
# print count1
# print count2
# print results1
# print results2
print results1
print results2
查找两个表之间匹配值的合适方法是在SQL中使用联接。然而,我认为这更像是Python中的练习,而不是SQL。
您的主要问题是您有嵌套的循环。考虑一下你的循环有效地做了什么:
list_a = ['a','b','c']
list_b = ['a','b','c']
for a_item in list_a:
for b_item in list_b:
print a_item,b_item
输出:
a a
a b
a c
b a
b b
b c
c a
c b
c c
如果您希望每个可迭代对象的长度相同,并且元素位于相同的位置,那么您可以执行更类似的操作:
for index in range(0,len(list_a)):
if list_a[index] == list_b[index]:
print 'Found'
continue
print 'Not Found'
您可以/应该对代码进行一些其他更改,但这些更改与您提出的问题并不直接相关。如果你想得到这些方面的反馈,请告诉我。
使用SQL
免责声明:你没有和我分享每个表都有哪些列,但这应该(希望)给你一个概念:
import psycopg2
import psycopg2.extras
conn = psycopg2.connect("host='localhost' dbname='dvdrental' user='postgres' password='jai'")
sql = """
SELECT
a.Col1 AS Col1_a,
b.Col1 AS Col1_b,
CASE
WHEN a.Col1 IS NOT NULL AND b.Col1 IS NOT NULL THEN 'Match'
ELSE 'No Match'
END AS Result
FROM
public.actor_1 a
FULL OUTER JOIN
public.actor_2 b
ON (b.Col1 = a.Col1);
"""
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute(sql)
results = cur.fetchall()
cur.close()
conn.close()
for row in results:
print row['Col1_a'], row['Col1_b'], row['result']
我已经尝试了类似以下带有扩展的代码。请让我知道你的反馈
import psycopg2
import numpy as np
conn_string = "host='localhost' dbname='dvdrental' user='postgres' password='jai'"
db1 = psycopg2.connect(conn_string)
db2= psycopg2.connect(conn_string)
cursor1=db1.cursor()
cursor2=db2.cursor()
cursor1.execute("select * from public.actor except select * from public.actor_1")
results1 = cursor1.fetchall()
cursor2.execute("select * from public.actor_1 except select * from public.actor")
results2 = cursor2.fetchall()
#count1 = len(results1)
#count2 = len(results2)
df1['results1']=df2['results2']
df1['not_match_match']=np.其中(df1['结果1']=df2['结果2'],'真','假')final=np.其中(df1)[1]打印(最终)