欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 兩個數(shù)據庫postgresql對比

 更新時間:2019年10月21日 10:38:18   作者:百變小超  
這篇文章主要介紹了python 兩個數(shù)據庫postgresql對比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了python 兩個數(shù)據庫postgresql對比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

比較兩個postgresql數(shù)據庫,原理 比較數(shù)據庫中各表的大小

1. 數(shù)據庫查詢語句

2. python字典比較

import psycopg2
import sys


class PdbModel:
  def __init__(self, host, dbname, username='postgres', password='postgres', port='5432'):
    self.host = host
    self.dbname = dbname
    self.username = username
    self.password = password
    self.port = port

    self.conn = None
    self.cursor = None
    self.init_db()

  def init_db(self):
    try:
      self.conn = psycopg2.connect(database=self.dbname, user=self.username, password=self.password,
                     host=self.host,
                     port="5432")
      self.cursor = self.conn.cursor()

    except Exception, e:
      error_out_print("Error: connection to db %s : %s failed. check need" % (self.host, self.dbname))
      print e
      sys.exit(-1)

  def execute_sql(self, sql, is_exist=True):
    """
      execute sql and return rows
    :param sql:
    :return:
      results of execute sql
    """
    try:
      standout_print('command sql : %s' % sql)
      self.cursor.execute(sql)
      rows = self.cursor.fetchall()
      return rows

    except Exception, e:
      self.conn.rollback()
      error_out_print("Failed: failed execute sql [%s]" % sql)
      error_out_print(e)
      if is_exist:
        self.close()
        sys.exit(-1)
      else:
        return None

  def get_tables_size(self):
    """
    select table_schema || '.' || table_name as table_full_name , pg_size_pretty(pg_total_relation_size('"'||table_schema||'"."'||table_name||'"')) as size
from information_schema.tables
order by pg_total_relation_size('"'||table_schema||'"."'||table_name||'"') DESC

    :return:
    """
    standout_print("get the size of tables in db [%s]." % self.dbname)
    sql = """ 
    select table_schema || '.' || table_name as table_full_name , pg_size_pretty(pg_total_relation_size('"'||table_schema||'"."'||table_name||'"')) as size
from information_schema.tables
order by pg_total_relation_size('"'||table_schema||'"."'||table_name||'"') DESC
    """
    rows = self.execute_sql(sql)
    table_size_dic = {}
    for row in rows:
      table_name = row[0]
      table_size = row[1]
      table_size_dic[table_name] = table_size
    return table_size_dic


def standout_print(info):
  sys.stdout.write("Info: %s " % info)
  sys.stdout.flush()


def error_out_print(info):
  sys.stderr.write("Error: %s " % info)
  sys.stderr.flush()


if __name__ == '__main__':
  db1 = ''
  db2 = ''
  host = "172.16.101.92"
  db_model1 = PdbModel(host, db1)
  db_model2 = PdbModel(host, db2)
  table_size_dic1 = db_model1.get_tables_size()
  table_size_dic2 = db_model2.get_tables_size()
  import pprint

  # pprint.pprint(table_size_dic1)
  # pprint.pprint(table_size_dic2)
  print cmp(table_size_dic1, table_size_dic2)
  is_equal = cmp(table_size_dic1, table_size_dic2)
  different_table_size_dic = {}
  if is_equal == 0:
    print "these tables in two database are same."
  else:
    keys1 = table_size_dic1.keys()
    keys2 = table_size_dic2.keys()

    for key in keys1:
      value1 = table_size_dic1.get(key)
      value2 = table_size_dic2.get(key)
      if cmp(value1, value2) != 0:
        different_table_size_dic[key] = (value1,value2)

  pprint.pprint(different_table_size_dic)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • python scrapy爬蟲代碼及填坑

    python scrapy爬蟲代碼及填坑

    這篇文章主要介紹了python scrapy爬蟲代碼及填坑,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • django之對FileField字段的upload_to的設定方法

    django之對FileField字段的upload_to的設定方法

    今天小編就為大家分享一篇django之對FileField字段的upload_to的設定方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • pytorch教程resnet.py的實現(xiàn)文件源碼分析

    pytorch教程resnet.py的實現(xiàn)文件源碼分析

    torchvision.models這個包中包含alexnet、densenet、inception、resnet、squeezenet、vgg等常用的網絡結構,并且提供了預訓練模型,可以通過簡單調用來讀取網絡結構和預訓練模型
    2021-09-09
  • 簡單學習Python time模塊

    簡單學習Python time模塊

    這篇文章主要和大家一起簡單學習一下Python time模塊,Python time模塊提供了一些用于管理時間和日期的C庫函數(shù),對time模塊感興趣的小伙伴們可以參考一下
    2016-04-04
  • Python設計模式中的策略模式詳解

    Python設計模式中的策略模式詳解

    策略模式屬于Python-設計模式中行為模式之一,該模式定義了一系列算法,并將每個算法封裝起來,使它們可以相互替換。本文將通過示例詳細講解這一模式,需要的可以參考一下
    2023-02-02
  • Python3如何使用range函數(shù)替代xrange函數(shù)

    Python3如何使用range函數(shù)替代xrange函數(shù)

    這篇文章主要介紹了Python3如何使用range函數(shù)替代xrange函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • Python實現(xiàn)Linux下守護進程的編寫方法

    Python實現(xiàn)Linux下守護進程的編寫方法

    這篇文章主要介紹了Python實現(xiàn)Linux下守護進程的編寫方法,比較實用的一個技巧,需要的朋友可以參考下
    2014-08-08
  • Python黑魔法@property裝飾器的使用技巧解析

    Python黑魔法@property裝飾器的使用技巧解析

    @property裝飾器能把一個方法變成屬性一樣來調用,下面我們就一起來看看Python黑魔法@property裝飾器的使用技巧解析
    2016-06-06
  • Python操作Excel插入刪除行的方法

    Python操作Excel插入刪除行的方法

    今天小編就為大家分享一篇Python操作Excel插入刪除行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python中的pickle模塊常用函數(shù)

    Python中的pickle模塊常用函數(shù)

    這篇文章主要介紹了Python中的pickle模塊常用函數(shù),pickle模塊使用的數(shù)據格式是python專用的,能夠把python對象直接保存到文件,而不需要轉化為字符串,也不用底層的文件訪問操作把它們寫入到一個二進制文件中,需要的朋友可以參考下
    2023-09-09

最新評論