我正在迁移数据库。我将源和目标表列表以及行计数存储在字典中。我需要比较两个字典,以查明表和数据是否已成功迁移
代码在下面被剪掉
sourceRowData = {'table1': 5, 'table2': 10, 'table3': 20, 'table4': 50}
targetRowData = {'table1': 5, 'table2': 10, 'table3': 8}
results = {}
# Compare keys
if set(sourceRowData.keys()) == set(targetRowData.keys()):
for key in sourceRowData.keys():
# Compare Values
if sourceRowData[key] != targetRowData[key]:
print ("Row count for table {} do not match".format(key))
results["Table Row Count"] = "Failed"
else:
results["Table Row Count"] = "Pass"
else:
results["Table Row Count"] = "Failed"
# Display which table is missing based on the missing key
for key, value in sourceRowData.items():
if not key in targetRowData.keys():
print ("Table {} not found in target database {}".format(key, targetDatabase))
我想同时比较键值和相应值。如果值(行数)不匹配,那么我想显示哪个表和行数不匹配。
我的代码为每个丢失的表打印消息,相反,我想显示行计数不匹配一次,然后将计数不匹配的每个表打印到控制台。和比较键时一样
包含验证报告的我的结果字典会针对每条效率不高的记录不断更新。
答案 0 :(得分:0)
这是一种方法。
例如:
sourceRowData = {'table1': 5, 'table2': 10, 'table3': 20, 'table4': 50}
targetRowData = {'table1': 5, 'table2': 10, 'table3': 8}
for key, v in sourceRowData.items():
if key not in targetRowData: #Check if table in targetRowData
print ("Table {} not found in target database {}".format(key, "targetDatabase"))
else:
if v != targetRowData[key]: #Check for count mismatch
print("Failed: Table Row Count mismatch {}".format(key))
输出:
Failed: Table Row Count mismatch table3
Table table4 not found in target database targetDatabase