从mssql迁移到postgresql(2)

185 阅读1分钟
数据迁移之后需要做的是数据校正,这里使用python+数据库模块实现
  1. 连接数据库
    1. PG
      1. psycopg2
    2. MSSQL
      1. pymssql,已停止维护
      2. pyodbc
  2. 使用pandas读取
    1. read_sql
  3. 比较两个dataframe是否相等
    1. assert_frame_equal
  4. 代码

import psycopg2import pyodbcimport pandas as pdfrom pandas.testing import assert_frame_equal##  read pg dataconn1 = psycopg2.connect(database='cm_test', user='postgres',password='123456', host='localhost', port='5432')df1 = pd.read_sql('select * from account_category',conn1)df1.columns = list(range(df1.shape[1]))# print(df1)## read mssql dataconn2 = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=CM_BE;UID=sa;PWD=123456')df2 = pd.read_sql('select * from AccountCategory_LU',conn2)df2.columns = list(range(df2.shape[1]))# print(df2)## compare data from two databasesassert_frame_equal(df1, df2)