Created
November 23, 2023 01:37
-
-
Save tperalta82/8691c6208246b11979c26e9f910d7713 to your computer and use it in GitHub Desktop.
Mysql Table data Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import mysql.connector | |
| def get_table_data(host, port, user, password, database, table): | |
| connection = mysql.connector.connect( | |
| host=host, | |
| port=port, | |
| user=user, | |
| password=password, | |
| database=database | |
| ) | |
| cursor = connection.cursor() | |
| query = f"SELECT * FROM {table}" | |
| cursor.execute(query) | |
| data = cursor.fetchall() | |
| cursor.close() | |
| connection.close() | |
| return data | |
| def compare_tables(host1, port1, user1, password1, database1, table1, host2, port2, user2, password2, database2, table2): | |
| data1 = get_table_data(host1, port1, user1, password1, database1, table1) | |
| data2 = get_table_data(host2, port2, user2, password2, database2, table2) | |
| if data1 == data2: | |
| print("Tables are identical.") | |
| else: | |
| print("Differences found:") | |
| for row1, row2 in zip(data1, data2): | |
| if row1 != row2: | |
| print(f"DB1 Table Row in {table1}: {row1}") | |
| print(f"DB2 Table Row in {table2}: {row2}") | |
| print("---") | |
| if __name__ == "__main__": | |
| # Replace the following with your actual connection details | |
| host1 = "somehost" | |
| user1 = "user" | |
| port1 = "3306" | |
| password1 = "pass" | |
| database1 = "db1" | |
| table1 = "t1" | |
| host2 = "somehost2" | |
| port2 = "3306" | |
| user2 = "user" | |
| password2 = "pass" | |
| database2 = "db2" | |
| table2 = "t2" | |
| compare_tables(host1, port1, user1, password1, database1, table1, host2, port2, user2, password2, database2, table2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment