Avoiding redundant SQL update in python

i want to keep track of the updates made by storing the increment value the table has been updated
however, the update scripts still execute even when there is no different values

This is the log:

Updated record for accession_id: KCH2400337322, test_type: 35, new test_status: 3, old test_status: 3
Updated record for accession_id: KCH2400337323, test_type: 35, new test_status: 3, old test_status: 3
Updated record for accession_id: KCH2400337324, test_type: 35, new test_status: 3, old test_status: 3
Updated record for accession_id: KCH2400337325, test_type: 35, new test_status: 5, old test_status: 5
Updated record for accession_id: KCH2400337327, test_type: 35, new test_status: 5, old test_status: 5
Updated record for accession_id: KCH2400337328, test_type: 40, new test_status: 3, old test_status: 3
Updated record for accession_id: KCH2400337329, test_type: 35, new test_status: 3, old test_status: 3
Updated record for accession_id: KCH2400337329, test_type: 40, new test_status: 2, old test_status: 2

i have tried rewrite the code… was facing the same issue on insert part so that is solved
using if and elif and using if else all yield the same results. and again explicitly stating that if existing status is equal to the fetched status it should continue does not work


this is updateEntry.py:
import mysql.connector
from helper import getDepartmentIdHelper, getTestTypeID
from config import testType1, testType2, testType3, testType4, interval

intvl = interval  # 100
department_id = getDepartmentIdHelper()  # 3
test_type_id1 = getTestTypeID(testType1)  # 35
test_type_id2 = getTestTypeID(testType2)  # 39
test_type_id3 = getTestTypeID(testType3)  # 40
test_type_id4 = getTestTypeID(testType4)  # 41

def updateEntries():
    try:
        # Connect to iBlissDB
        iblis_connection = mysql.connector.connect(
            host="127.0.0.1",
            port="3306",
            user="root",
            password="root",
            database="tests"
        )

        # Connect to srsDB
        srs_connection = mysql.connector.connect(
            host="127.0.0.1",
            port="3306",
            user="root",
            password="root",
            database="Haematology"
        )

        iblis_cursor = iblis_connection.cursor(dictionary=True)
        srs_cursor = srs_connection.cursor(dictionary=True)

        # iblis_query to fetch the required data
        iblis_query = """
        WITH RankedTests AS (
            SELECT 
                specimens.accession_number AS accession_id,
                tests.test_type_id AS test_type,
                tests.test_status_id AS test_status,
                ROW_NUMBER() OVER (
                    PARTITION BY specimens.accession_number, tests.test_type_id 
                    ORDER BY tests.time_created DESC
                ) AS rn
            FROM 
                specimens
            INNER JOIN 
                tests ON specimens.id = tests.specimen_id
            WHERE 
                specimens.specimen_type_id = %s
                AND tests.test_status_id NOT IN (1, 6, 7, 8)
                AND tests.time_created >= NOW() - INTERVAL %s DAY
                AND tests.test_type_id IN (%s, %s, %s, %s)
        )
        SELECT
            accession_id,
            test_type,
            test_status
        FROM
            RankedTests
        WHERE
            rn = 1;
        """

        # Execute the query
        iblis_cursor.execute(iblis_query, (department_id, intvl, test_type_id1, test_type_id2, test_type_id3, test_type_id4))
        iblis_results = iblis_cursor.fetchall()

        # Insert the results into srsDB if they don't already exist
        for result in iblis_results:
            accession_id = result['accession_id']
            test_type = result['test_type']
            test_status = result['test_status']

            # Check if accession_id with the same test_type already exists in the srsDB tests table
            srs_cursor.execute("SELECT test_status FROM tests WHERE accession_id = %s AND test_type = %s", (accession_id, test_type))
            existing_record = srs_cursor.fetchone()

            if not existing_record:
                # Insert the record into srsDB
                srs_insert_query = """
                INSERT INTO tests (accession_id, test_type, test_status)
                VALUES (%s, %s, %s)
                """
                srs_cursor.execute(srs_insert_query, (accession_id, test_type, test_status))
                srs_connection.commit()
                print(f"Inserted new record for accession_id: {accession_id}, test_type: {test_type}, test_status: {test_status}")

            elif existing_record['test_status'] == test_status:
                print(f"No update needed for accession_id: {accession_id}, test_type: {test_type}, test_status: {test_status}")
                continue

            elif existing_record['test_status'] != test_status:
                # Update the status if it is different
                srs_update_query = """
                UPDATE tests
                SET test_status = %s
                WHERE accession_id = %s AND test_type = %s
                """
                srs_cursor.execute(srs_update_query, (test_status, accession_id, test_type))
                srs_connection.commit()
                print(f"Updated record for accession_id: {accession_id}, test_type: {test_type}, new test_status: {test_status}, old test_status: {existing_record['test_status']}")

    except mysql.connector.Error as err:
        print(f"Error: {err}")

    finally:
        # Close all connections and cursors
        if 'iblis_cursor' in locals():
            iblis_cursor.close()
        if 'iblis_connection' in locals():
            iblis_connection.close()
        if 'srs_cursor' in locals():
            srs_cursor.close()
        if 'srs_connection' in locals():
            srs_connection.close()

updateEntries()

i have tried rewrite the code… was facing the same issue on insert part so that is solved
using if and elif and using if else all yield the same results. and again explicitly stating that if existing status is equal to the fetched status it should continue does not work

New contributor

89Tain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật