Voll il y a 9 ans
Parent
commit
c95589ce03
5 fichiers modifiés avec 220 ajouts et 0 suppressions
  1. 74 0
      Diff.py
  2. 95 0
      MSSQL.py
  3. BIN
      __pycache__/Diff.cpython-34.pyc
  4. BIN
      __pycache__/MSSQL.cpython-34.pyc
  5. 51 0
      show.py

+ 74 - 0
Diff.py

@@ -0,0 +1,74 @@
+#coding=utf-8
+
+import MSSQL
+
+db2010 = MSSQL.MSSQL(host=r"192.168.1.100",user="sa",pwd="`12345",db="Con2010_zjhw")
+db2016 = MSSQL.MSSQL(host=r"192.168.1.100\MSSQLSERVERR2",user="sa", \
+                     pwd="`12345",db="Con2016")
+
+def FindNotIn():
+    
+    sqlalltable=r"SELECT Name FROM SysObjects Where XType='U' ORDER BY Name"
+    
+    tablelist2010 = ToList(db2010.ExecQuery(sqlalltable))
+    tablelist2016 = ToList(db2016.ExecQuery(sqlalltable)) 
+
+    TnotIn=[]
+    CnotIn={}
+    
+    for table in tablelist2010:
+        if table not in tablelist2016:
+            TnotIn.append(table)
+        else:
+            sqlallCol=r"SELECT Name FROM SysColumns WHERE id=Object_Id('%s')" %table
+            col2010=ToList(db2010.ExecQuery(sqlallCol))
+            col2016=ToList(db2016.ExecQuery(sqlallCol))
+
+            collist=[]
+            for col in col2010:
+                if col not in col2016:
+                    collist.append(col)
+            if len(collist)>0:
+                CnotIn[table]=collist
+    return TnotIn,CnotIn
+
+def FindNotIn_TR_P_FN(TR_P_FN):
+    sql="SELECT * FROM Sysobjects WHERE xtype = '%s'" %TR_P_FN
+    list2010 = ToList(db2010.ExecQuery(sql))
+    list2016 = ToList(db2016.ExecQuery(sql))
+
+    notin=[]
+    notsame=[]
+    
+    for x in list2010:
+        if x not in list2016:
+            notin.append(x)
+        else:
+            sqltxt="EXEC sp_helptext '%s'" %x
+            txt2010=ToStr(db2010.ExecQuery(sqltxt))
+            txt2016=ToStr(db2016.ExecQuery(sqltxt))
+            if txt2010!=txt2016:
+                notsame.append(x)
+    return notin,notsame
+    
+
+def ToList(listresult):
+    result=[]
+    for x in listresult:
+        result.append (x[0])   
+    return result
+
+def ToStr(listresult):
+    result=""
+    for x in listresult:
+        result+=x[0].strip()
+    return result
+
+if __name__ == '__main__':   
+    t10=ToStr(db2010.ExecQuery("EXEC sp_helptext 'sp_qcc010305'"))
+    t16=ToStr(db2016.ExecQuery("EXEC sp_helptext 'sp_qcc010305'"))
+    if t10==t16:
+        print("ok")
+    else:
+        print("no")
+    

+ 95 - 0
MSSQL.py

@@ -0,0 +1,95 @@
+#coding=utf-8 
+#!/usr/bin/env python
+#-------------------------------------------------------------------------------
+# Name: pymssqlTest.py
+# Purpose: 测试 pymssql库,该库到这里下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql
+#
+# Author: scott
+#
+# Created: 04/02/2012
+#-------------------------------------------------------------------------------
+
+import pymssql
+
+
+class MSSQL:
+    """
+    对pymssql的简单封装
+    pymssql库,该库到这里下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql
+    使用该库时,需要在Sql Server Configuration Manager里面将TCP/IP协议开启
+
+    用法:
+
+    """
+
+    def __init__(self,host,user,pwd,db):
+        self.host = host
+        self.user = user
+        self.pwd = pwd
+        self.db = db
+
+    def __GetConnect(self):
+        """
+        得到连接信息
+        返回: conn.cursor()
+        """
+        if not self.db:
+            raise(NameError,"没有设置数据库信息")
+        self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8")
+        cur = self.conn.cursor()
+        if not cur:
+            raise(NameError,"连接数据库失败")
+        else:
+            return cur
+
+    def ExecQuery(self,sql):
+        """
+        执行查询语句
+        返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段
+
+        调用示例:
+                ms = MSSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics")
+                resList = ms.ExecQuery("SELECT id,NickName FROM WeiBoUser")
+                for (id,NickName) in resList:
+                    print str(id),NickName
+        """
+        try:
+            
+            cur = self.__GetConnect()
+            cur.execute(sql)
+            resList = cur.fetchall()
+        except Exception as e:
+             print(e) 
+        finally:
+            #查询完毕后必须关闭连接
+            self.conn.close()
+        return resList
+
+    def ExecNonQuery(self,sql):
+        """
+        执行非查询语句
+
+        调用示例:
+            cur = self.__GetConnect()
+            cur.execute(sql)
+            self.conn.commit()
+            self.conn.close()
+        """
+        cur = self.__GetConnect()
+        cur.execute(sql)
+        self.conn.commit()
+        self.conn.close()
+
+def main():
+## ms = MSSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics")
+## #返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段
+## ms.ExecNonQuery("insert into WeiBoUser values('2','3')")
+
+    ms = MSSQL(host="192.168.1.100",user="sa",pwd="`12345",db="Con2010_zjhw")
+    resList = ms.ExecQuery("SELECT * from comp")
+    print(resList)
+    #for (id,weibocontent) in resList:
+    #    print str(weibocontent).decode("utf8")
+
+if __name__ == '__main__':
+    main()

BIN
__pycache__/Diff.cpython-34.pyc


BIN
__pycache__/MSSQL.cpython-34.pyc


+ 51 - 0
show.py

@@ -0,0 +1,51 @@
+#coding=utf-8
+
+from Diff import *
+
+
+
+if __name__ == '__main__':
+
+    #table
+    tables,cols=FindNotIn()
+    print("\n2016缺少的表:")
+    print("-"*10)
+    for t in tables:
+        print("- "+t)
+    print("\n2016缺少的字段:")
+    print("-"*10)
+    for c in cols.items():
+        print("- %s:%s" %(c[0],c[1]))
+
+    #tri
+    tri,trisame=FindNotIn_TR_P_FN("TR")
+    print("\n2016缺少的触发器:")
+    print("-"*10)
+    for t in tri:
+        print("- "+t)
+    print("\n与2016不一样的触发器:")
+    print("-"*10)
+    for t in trisame:
+        print("- "+t)
+
+    #proc
+    proc,procsame=FindNotIn_TR_P_FN("P")
+    print("\n2016缺少的存储过程:")
+    print("-"*10)
+    for p in proc:
+        print("- "+p)
+    print("\n与2016不一样的存储过程:")
+    print("-"*10)
+    for p in procsame:
+        print("- "+p)
+
+    #FN
+    fn,fnsame=FindNotIn_TR_P_FN("FN")
+    print("\n2016缺少的函数:")
+    print("-"*10)
+    for f in fn:
+        print("- "+f)
+    print("\n与2016不一样的函数:")
+    print("-"*10)
+    for f in fnsame:
+        print("- "+f)