Sometimes you need to compare two SQL databases for presence of similar tables or data.
This is frequently done when your application depends on certain table/data structure and you need to upgrade it or your application fetches data/structure from a cloud and needs to ensure consistency.
The program below could serve as a starting template when you need such comparison.
We will start off with comparing table presence, i.e. database 1 has same tables as database 2:
This is frequently done when your application depends on certain table/data structure and you need to upgrade it or your application fetches data/structure from a cloud and needs to ensure consistency.
The program below could serve as a starting template when you need such comparison.
We will start off with comparing table presence, i.e. database 1 has same tables as database 2:
1.sqlite |
2.sqlite |
3.sqlite |
import sqlite3 db1 = "1.sqlite" #db2 = "2.sqlite" #comment this line for negative test db2 = "3.sqlite" #comment this line for positive test tableCompare = "SELECT name FROM sqlite_master WHERE type='table' order by name" print("....Connecting to Database "+db1) conn1 = sqlite3.connect(db1) print("....Connecting to Database "+db2) conn2 = sqlite3.connect(db2) cursor1 = conn1.cursor() result1 = cursor1.execute(tableCompare) cursor2 = conn2.cursor() result2 = cursor2.execute(tableCompare) print("....Comparing Tables") for row1 in result1: row2 = result2.fetchone() print(row1) print(row2) if row1 is not None and row2 is not None and (row1[0] == row2[0]): print("........Tables Match:"+ row1[0]) else: if (row1 is not None and row1[0] is not None): print("!!!!!!!!PROBLEM "+db1+" is missing Table:" + row1[0]) else: print("!!!!!!!!PROBLEM "+db2+" is missing Table:" + row2[0]) print("........Fix the problem and restart this comparator") exit() print("....Done comparing table presence")