I’m Working on a Real-time Data Mirroring From MySQL to PostgreSQL
I’m using Python for that kinda project.
Code works Just fine but,
The Problem is when I keep it running it’s only updating old data but don’t Add new Records that been added to MySQL Database
here’s Full Code.
import timeimport pyodbcimport psycopg2import mysql.connector# Connect to MySQLmysql_conn = mysql.connector.connect( host="localhost", user="root", password="******", database="ShagufTask")# Connect to PostgreSQLpg_conn = psycopg2.connect(host="localhost", database="ShagufSecondTask", user="postgres", password="******")def mirror_data(): try: # Retrieve data from MySQL mysql_cursor = mysql_conn.cursor() mysql_query = "SELECT id, name, city, revenue FROM myapp_shaguftask" mysql_cursor.execute(mysql_query) data_to_mirror = mysql_cursor.fetchall() # Insert or update data into PostgreSQL pg_cursor = pg_conn.cursor() for row in data_to_mirror: pg_cursor.execute("SELECT id FROM shaguftable WHERE id = %s", (row[0],)) existing_record = pg_cursor.fetchone() if existing_record: pg_cursor.execute("UPDATE shaguftable SET name = %s, city = %s, revenue = %s WHERE id = %s", (row[1], row[2], row[3], row[0])) print("Data Updated") print(row[0]) else: pg_cursor.execute("INSERT INTO shaguftable (id, name, city, revenue) VALUES (%s, %s, %s, %s)", row) print("Data Inserted") print(row[0]) # Commit changes pg_conn.commit() print("Data mirroring completed successfully.") except Exception as e: print("An error occurred:", str(e))# Continuously run the mirroring scriptwhile True: mirror_data() # Sleep for a specified interval before fetching new data again time.sleep(10) # Sleep for 1 minute before fetching new data
I think the Problem that it’s not Capable of reading new Records while it’s Running but I don’t know how to Solve this Problem