Я уже выполнил перенос данных с сервера SQL на Postgres. Здесь я могу сравнить разницу строк между обеими таблицами в разных базах данных. Я добавил новую строку в таблицу SQL Server. Теперь я хочу, чтобы в таблице Postgres обновлялась только эта новая строка, а не вся таблица.
import pandas as pd
from sqlalchemy import create_engine
def ask_mssql():
engine = create_engine('mssql+pyodbc://@' + 'RONY' + '/' + 'testdb' + '?driver=SQL+Server')
return engine
def ask_postgre():
db_string = "postgres+psycopg2://{}:{}@{}/{}".format('postgres', 'rony1234',
'127.0.0.1',
'mockdb')
engine = create_engine(db_string)
return engine
def mssql_syntax(diff, tablename, order):
query = 'select top {} * from {} order by {} desc'.format(diff, tablename, order)
return query
def postgre_syntax(diff, tablename, order):
query = 'select * from {} order by {} desc limit {}'.format(tablename, order, diff)
return query
if __name__ == '__main__':
db_type = input("Please enter client's db: mssql, postgresql, oracle\n")
db_type = db_type.lower()
if db_type == 'mssql':
engine_client = ask_mssql()
db_type_our = input("Please enter your db: mssql, postgresql, oracle\n")
db_type_our = db_type_our.lower()
if db_type_our == 'postgresql':
engine_our = ask_postgre()
table_name = input('\nInput the client table:\n')
query = 'select count(*) from {}'.format(table_name)
df_client = pd.read_sql(query,engine_client)
our_table = input('\nInput the table name in your db\n')
pquery = 'select count(*) from {}'.format(our_table)
df_our = pd.read_sql(pquery,engine_our)
if df_client.values[0][0] != df_our.values[0][0]:
diff = df_client.values[0][0] - df_our.values[0][0]
print('\nDifference of {} rows found'.format(diff))
order_column = input('\nPlease enter a column to order the data on\n')
if db_type == 'mssql':
query = mssql_syntax(diff, table_name, order_column)
if db_type == 'postgresql':
query = postgre_syntax(diff, table_name, order_column)
df = pd.read_sql(query, engine_client)
df.to_sql(our_table, engine_our, if_exists='append')
else:
print('\nTable already Updated.')