一个简单比较oracle两个用户下表数据差异的python脚本

    分享一个脚本,用于比较数据差异。


    日常工作中经常遇到这么一种情况,在数据迁移或者备份数据恢复等工作时,必须要比较两个用户下表的数据(主要是条数)是否一致,是否有漏数据的情况。虽然使用sqlplus+shell可以很好的完成这项工作,但是为了学习python,还是写成了python版的比对脚本,可以执行一些数据库操作并在执行目录生成result.txt的结果,明明白白的指出两个用户下表的不同,需要的童鞋可以拿来试一下,如果有疑问或者错误,欢迎指正~

    此脚本执行需要python3.x(稍作修改在2.7执行),cx_Oracle(用于连接oracle),oracle客户端等


    脚本执行完成,会在执行目录生成结果文件(原谅我的蹩脚英语)。



#!/bin/python
# -*- coding: UTF-8 -*-


import cx_Oracle as orcl
import sys
 
#print 'Number of arguments:', len(sys.argv), 'arguments.'
#print 'Argument List:', str(sys.argv)



#This func is used to read data from database and write them to list
def read_tbname(conn, list):
  cursor = conn.cursor()
  cursor.execute(sql_tbs)
  result = cursor.fetchall()
  row_count = cursor.rowcount
  for row in result:
    list.append(row)
  cursor.close()

#This func is used to read data from database and write them to dictionary
def read_count(conn, list, dict):
  cursor = conn.cursor()
  for li in list:
    sql = sql_count+"".join(li)
    cursor.execute(sql)
    result = cursor.fetchall()
    dict.setdefault(li,[]).append(result)
  cursor.close()

#compare the data
def compare_data(src_dict, dest_dict, flag=0):
  global num
  for key, value in src_dict.items():
    dest_value = dest_dict.get(key)
    if dest_value is None:
      if flag==0:
        print_to_file("Table_name: "+"".join(key)+" is not exists in target,and source count is: "+str(value[0][0][0]))
      else:
        print_to_file("Table_name: "+"".join(key)+" is not exists in source,and target count is: "+str(value[0][0][0]))
        
      num += 1
    elif value != dest_value and flag==0:
      num += 1

      src_str = str(value[0][0][0])
      dest_str = str(dest_value[0][0][0])
      str1 = "Number in table_name: "+"".join(key)+" is not the same with source and target, source count: "+src_str+", dest count: "+dest_str
      print_to_file(str1)




def print_to_file(sth):
  #print to the file
  sth = sth.ljust(106)
  sth = sth.ljust(107,'#')
  ret_file.write("# "+sth+"\n")



def final_func():
  #close file
  ret_file.close()  


if __name__=='__main__':  


  #The str of source database
  src_host = "192.168.56.10"
  src_port = "1521"
  src_sid = "orcl"
  src_uname = "TEST"
  src_password = "oracle"
  src_dsn = orcl.makedsn(src_host, src_port, src_sid)
  src_conn = orcl.connect(src_uname, src_password, src_dsn)
  
  #The str of target database
  dest_host = "192.168.56.10"
  dest_port = "1521"
  dest_sid = "orcl"
  dest_uname = "MD"
  dest_password = "md"
  dest_dsn = orcl.makedsn(src_host, src_port, src_sid)
  dest_conn = orcl.connect(dest_uname, dest_password, dest_dsn)

  #used to store source and target datas
  src_dict = {}
  dest_dict = {}
  #used to store table_name
  src_list = []
  dest_list = []

  #used to store the diff result
  ret_file = open('result.txt','w+',1024)
  #sql statement
  sql_tbs = "select table_name from user_tables"
  sql_count = "select count(*) from "
  
  #if source is the same as target,then num is zero
  num = 0

  
  read_tbname(src_conn, src_list) 
  read_tbname(dest_conn, dest_list) 
  
  read_count(src_conn, src_list, src_dict)
  read_count(dest_conn, dest_list, dest_dict)
 
#  print '\033[1;31;40m' 
  ret_file.write("#############################################################################################################\n")
  compare_data(src_dict, dest_dict)

  ret_file.write("#############################################################################################################\n")
  ret_file.write("######################          target table not exists in source               #############################\n")

  compare_data(dest_dict, src_dict, 1)
  ret_file.write("#############################################################################################################\n")

  if num == 0:
    print_to_file("The dest is the same as source!\n")
    ret_file.write("#############################################################################################################\n")
  
  final_func()
 
  print ("Succeed Complete!")

#  print '\033[0m'


你可能感兴趣的:(python)